Question

I have two xts time series in R and want to calculate the differences between the values of the time series at the times which are closest to each other. That is, if my two indices are:

[1] (10/10/05 13:00:00) (10/10/05 14:00:00) (10/10/05 14:23:00)

and

[1] (10/10/05 12:38:00) (10/10/05 12:53:00) (10/10/05 12:59:00) (10/10/05 13:08:00) (10/10/05 13:23:00)
[6] (10/10/05 13:38:00) (10/10/05 13:53:00) (10/10/05 14:23:00) (10/10/05 15:05:00) (10/10/05 15:11:00)

I want to calculate the differences of the values at:

  • 13:00 and 12:59
  • 14:00 and 13:53
  • 14:30 and 14:23

How should I do this? The standard merge method from zoo with all=FALSE won't do what I want because the indices have to be exactly equal for it to merge properly.

Any ideas?

Was it helpful?

Solution

You didn't provide a reproducible example, so I can't give you a specific solution. Generally, you can use align.time to change the index values of each object to a similar periodicity, or you could merge and use na.locf to fill the missing values. Then you can do whatever operations you want between the two series.

OTHER TIPS

I am thinking about something like this: for each member of the first series, you insert it in the second series according to their time index and then compute the absolute differences between the indices of the inserted first series member and that of the preceding and following second series members, taking the corresponding difference between values for which the absolute difference between indices is the smallest.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top