I have 100 text files which contains time series starting and ending at different point of times. I want to extract the values for common period of time in the series.
Use the following code to generate the sample data:
set.seed(1)
D1 = data.frame(time = seq(ISOdatetime(2012, 6, 26, 3, 15, 00),
length = 500, by = 900),
value = rnorm(500))
D2 = data.frame(time = seq(ISOdatetime(2012, 6, 24, 5, 30, 00),
length = 541, by = 900),
value = rnorm(541))
D3 = data.frame(time = seq(ISOdatetime(2012, 6, 23, 5, 45, 00),
length = 700, by = 900),
value = rnorm(700))
This data will give you 3 time series starting and ending and different times. I wish to keep only the values for common time period and remove the rest. i.e.
if,
- 1st series starts with “2012-6-26 3:45:26” ends with “2012-8-07 4:45:26”
- 2nd with “2012-6-24 5:55:27” ends with “2012-7-28 7:45:26”
- 3rd with “2012-6-23 5:04:30” ends with “2012-7-27 4:45:26”
Then I wish to keep the data of intersection of the three time series i.e. data corresponding to:-
- start: “2012-6-26 3:45:26”
- end:”2012-7-27 4:45:26″
- for all the 3 series and remove the rest.
I searched SO and other websites but didn’t find any solution. Need help on that.
How do I achieve that?
It seems like you need to familiarize yourself with the
xtspackage. Convert your data frame toxtstime series objects and usemerge.mergewill merge all values, so if you want values occurring in all, you can also usena.omit.Here is some example output. For
headandtail, note the presence ofNAvalues.