I’m not understanding how to create a new ‘lagged’ column in a data.frame. My current data is collected at the end of the data. One program I need to send this to assumes it’s collected first thing in the morning, so I need to lag column 2 by 1 row. The code I wrote just returns the same data.
How can I do this properly?
Thanks.
D8 = structure(list(Date = structure(c(14396, 14397, 14398, 14399,
14400, 14403, 14404, 14405, 14406,
14407, 14410, 14411, 14412, 14413,
14414, 14417, 14418, 14419, 14420,
14421, 14424, 14425, 14426, 14427,
14428, 14431, 14432, 14433, 14434,
14435), class = "Date"),
PL8 = c(0, 0, 0, 0, 76, 0, -334, -974, -104, 356, 378, -1102,
-434, 266, -434, 444, 464, 0, 486, 406, -224, -214, 0, -4,
0, -188, 356, 322, -484, 436)), .Names = c("Date", "PL8"), row.names =
c(NA, 30L), class = "data.frame")
D8
D8[,3] = lag(D8[,2],k=-1)
D8
Try this:
It would be a bit easier if you used a time series class. In that case you could use
lag:In the other direction we would have:
and