Currently, I have an xts
time series, called Data
, which contains a Date
, and two value columns, Value1
and Value2
which are both numbers. I would like to get a single number as output from my code which would be the mean of Value1
in the time period from a point where Value2 < mean(Value2)
and going forward 14 data points, weeks in this particular data set.
In order to get the dates where Value2 < mean(Value2)
, I wrote the below code
Data[which(Data$Value2 < mean(Data$Value2)),"Date"]
However, I am not sure how to get the mean of Value1
in the period, going 14 days forward from each of the resultant dates from the above code.
Example Dataset:
Value1 Value2
1 2009-01-02 22.6500 17
2 2009-01-09 21.4700 56
3 2009-01-16 20.6100 -50
4 2009-01-23 19.6800 13
5 2009-01-30 19.2800 172
6 2009-02-06 20.1300 -120
7 2009-02-13 18.9900 17
The mean of Value2 is 12.57. Therefore the selected dates would be 2009-01-16 and 2009-02-06 since Value2 < mean(Value2) there. I would then like the mean of Value1 in the time period from 2009-01-16 to 2009-01-30 and from 2009-02-06 to 2009-02-20, in matrix form with the start date followed by the mean(Value1).