I have time-series (xts formatted) power consumption data at 10 minutes rate as
power
2015-08-01 00:00:00 101.22
2015-08-01 00:10:00 122.941
2015-08-01 00:20:00 67.596
2015-08-01 00:30:00 184.180
Now I want to add 3 more columns to it as:
- Column # 2: "Prevday1" - where "prevday1" will contain the power consumption readings of previous day at the same time. That is, if the current index is 5 August 2015, 1100 hours then "prevday1" should contain consumption of previous day at same time instant (4 August 2015, 1100 hours)
- Column # 3: "Prevday2" - where "prevday2" will contain the power consumption readings of day before previous day at the same time instant
- column # 4: "previnstant1" - where "previnstant1" will contain reading of previous time instant. In my case it will be power consumption before 10 minutes
Somehow new xts object will be like
power prevday1 prevday2 previnstant1
2015-08-01 00:00:00 101.22 NA NA NA
2015-08-01 00:10:00 122.941 : : :
2015-08-01 00:20:00 67.596
2015-08-01 00:30:00 184.180
:
Now the question is how should I extract the values for columns 2, 3 and 4 from the historical xts object. I started with .indexday type of functions but could not get the values. Is there any specific function in R to extract these type of values using xts indexes?
lag()returns the entire object back with some observations havingnaas specified. I need values corresponding to specific previous timestamps. - Haroon Rashid