更新XTS对象 [英] Updating an XTS object
问题描述
我一直在努力地做一些事情,在R上可能非常简单.我试图做的是使用另一个XTS对象来更新XTS对象列.
I've been struggling to do something is probably horribly simple on R. What I am trying to do is to update a XTS object column using another XTS object.
比方说,我有以下名为Object1的XTS对象:
Let's say I have the following XTS object, named Object1:
A B
2000-01-03 , 14, NA
2000-01-04 , NA, NA
2000-01-05 , 16, 100
2000-01-06 , NA, 200
以及以下名为Object2的XTS对象:
And the following XTS object, named Object2:
A
2000-01-05 , 160
2000-01-06 , 20
我正在寻找一种使用Object2中的值更新Object1的方法,其结果如下:
I am looking for a way to update Object1 with values from Object2, having the following result:
A B
2000-01-03 , 14, NA
2000-01-04 , NA, NA
2000-01-05 , 160, 100
2000-01-06 , 20, 200
如果我合并(Object1,Object2),我将拥有:
If I do merge(Object1,Object2), I will have:
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,所以这绝对不是我要找的东西.
Which is absolutely not what I'm looking for, since I am trying to update Object1$A based on Object2$A.
我在这里想念什么?
以下是dput(Object1)
和dput(Object2)
的输出,可重复使用:
Here is the output of dput(Object1)
and dput(Object2)
to make this reproducible:
> 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"))
推荐答案
您可以使用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
这篇关于更新XTS对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!