Method to compare previous day to current day values
<p>I am looking for a better way to compare a value from a day (day X) to the previous day (day X-1). Here I am using the <code>airquality</code> dataset. Suppose I am interested in comparing the wind from one day to the wind from the previous day. Right now I am using <code>merge()</code> to bring together two dataframes - one current day dataframe and one from the previous day. I am also just subtracting 1 from the <code>Day</code> column to get the <code>PrevDay</code> column:</p>

<pre><code>airquality$PrevDay=airquality$Day-1

airquality.comp &lt;- merge(
  airquality[,c("Wind","Day")],
  airquality[,c("Temp","PrevDay")], 
  by.x=c("Day"),by.y=c("PrevDay"))
</code></pre>

<p>My issue here is that I'd need to create another dataframe if I wanted to look back 2 days or if I wanted to switch <code>Wind</code> and <code>Temp</code> and look at them the other way. This just seems clunky. Can anyone recommend a better way of doing this?</p>