更新XTS对象

时间:2013-01-30 21:18:04

标签: r merge xts

我一直在努力做点什么在R上可能非常简单。我想要做的是使用另一个XTS对象更新XTS对象列。

假设我有以下XTS对象,名为Object1:

A B
2000-01-03 , 14, NA
2000-01-04 , NA, NA
2000-01-05 , 16, 100
2000-01-06 , NA, 200

以下XTS对象,名为Object2:

A
2000-01-05 , 160
2000-01-06 , 20

我正在寻找一种方法来使用Object2中的值更新Object1,具有以下结果:

A B
2000-01-03 , 14, NA
2000-01-04 , NA, NA
2000-01-05 , 160, 100
2000-01-06 , 20, 200

如果我合并(Object1,Object2),我会:

A B A.1
2000-01-03 , 14, NA, NA
2000-01-04 , NA, NA, NA
2000-01-05 , 16, 100, 160
2000-01-06 , NA, 200, 20

这绝对不是我想要的,因为我正在尝试基于Object2 $ A更新Object1 $ A.

我在这里缺少什么?

以下是dput(Object1)dput(Object2)的输出,以使其可重现:

> dput(Object1)
structure(c(14, NA, 16, NA, NA, NA, 100, 200), .Dim = c(4L, 2L
), index = structure(c(946857600, 946944000, 947030400, 947116800
), tzone = "UTC", tclass = "Date"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", .Dimnames = list(
    NULL, c("A", "B")), class = c("xts", "zoo"))

> dput(Object2)
structure(c(160, 20), .Dim = c(2L, 1L), index = structure(c(947030400, 
947116800), tzone = "UTC", tclass = "Date"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", .Dimnames = list(
    NULL, "A"), class = c("xts", "zoo"))

1 个答案:

答案 0 :(得分:4)

您可以使用Object1

的索引对Object2进行分组
> Object1[index(Object2), "A"] <- Object2$A
> Object1
             A   B
2000-01-03  14  NA
2000-01-04  NA  NA
2000-01-05 160 100
2000-01-06  20 200