当值处于不同时间时,计算两个xts时间序列的差异

时间:2022-06-15 05:24:26

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:

我在R中有两个xts时间序列,并且想要计算彼此最接近的时间序列值之间的差异。也就是说,如果我的两个索引是:

[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
  • 13:00和12:59
  • 14:00 and 13:53
  • 14:00和13:53
  • 14:30 and 14:23
  • 14:30和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.

我该怎么做?来自动物园的标准合并方法全部为= FALSE将不会执行我想要的操作,因为索引必须完全相等才能正确合并。

Any ideas?

有任何想法吗?

2 个解决方案

#1


1  

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.

您没有提供可重现的示例,因此我无法为您提供具体的解决方案。通常,您可以使用align.time将每个对象的索引值更改为类似的周期,或者您可以合并并使用na.locf来填充缺失的值。然后你可以在两个系列之间做任何你想做的操作。

#2


0  

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.

我正在考虑这样的事情:对于第一个系列的每个成员,根据它们的时间索引将它插入第二个系列,然后计算插入的第一个系列成员的索引与前面和后面的成员之间的绝对差异第二系列成员,取得指数之间的绝对差异最小的值之间的相应差异。

#1


1  

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.

您没有提供可重现的示例,因此我无法为您提供具体的解决方案。通常,您可以使用align.time将每个对象的索引值更改为类似的周期,或者您可以合并并使用na.locf来填充缺失的值。然后你可以在两个系列之间做任何你想做的操作。

#2


0  

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.

我正在考虑这样的事情:对于第一个系列的每个成员,根据它们的时间索引将它插入第二个系列,然后计算插入的第一个系列成员的索引与前面和后面的成员之间的绝对差异第二系列成员,取得指数之间的绝对差异最小的值之间的相应差异。