合并 XTS 数据时重复值
如果我的一个表有多个时间戳值,而另一个表只有一个,我可以重复多个值中单个项目的值吗?
例如:
XTS_A:
2011/01/01 10:00:00,Bar
2011/01/01 10:00:01,Baz
XTS_B:
2011/01/01 10:00:00,A
2011/01/01 10:00:00,B
2011/01/01 10:00:00,C
2011/01/01 10:00:01,B
Merge_Result:
2011/01/01 10:00:00,A,Bar
2011/01/01 10:00:00,B,Bar
2011/01/01 10:00:00,C,Bar
2011/01/01 10:00:01,B,Baz
可重现的示例:
library(zoo)
library(xts)
XTS_A <- structure(c("Bar", "Baz"), .Dim = c(2L, 1L), index = structure(c(1293894000, 1293894001), tzone = "", tclass = c("POSIXt", "POSIXct")), class = c("xts", "zoo"), .indexCLASS = c("POSIXt", "POSIXct"), .indexTZ = "")
XTS_B <- structure(c("A", "B", "C", "B"), .Dim = c(4L, 1L), index = structure(c(1293894000, 1293894000, 1293894000, 1293894001), tzone = "", tclass = c("POSIXt", "POSIXct")), class = c("xts", "zoo"), .indexCLASS = c("POSIXt", "POSIXct"), .indexTZ = "")
If one of my tables has multiple values for the time stamp, and the other just one, can I repeat the value of the single item in the multiple values?
For instance:
XTS_A:
2011/01/01 10:00:00,Bar
2011/01/01 10:00:01,Baz
XTS_B:
2011/01/01 10:00:00,A
2011/01/01 10:00:00,B
2011/01/01 10:00:00,C
2011/01/01 10:00:01,B
Merge_Result:
2011/01/01 10:00:00,A,Bar
2011/01/01 10:00:00,B,Bar
2011/01/01 10:00:00,C,Bar
2011/01/01 10:00:01,B,Baz
Reproducible example:
library(zoo)
library(xts)
XTS_A <- structure(c("Bar", "Baz"), .Dim = c(2L, 1L), index = structure(c(1293894000, 1293894001), tzone = "", tclass = c("POSIXt", "POSIXct")), class = c("xts", "zoo"), .indexCLASS = c("POSIXt", "POSIXct"), .indexTZ = "")
XTS_B <- structure(c("A", "B", "C", "B"), .Dim = c(4L, 1L), index = structure(c(1293894000, 1293894000, 1293894000, 1293894001), tzone = "", tclass = c("POSIXt", "POSIXct")), class = c("xts", "zoo"), .indexCLASS = c("POSIXt", "POSIXct"), .indexTZ = "")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那之后再填呢?有点丑陋的例子(使用循环,但很难避免像这样的顺序依赖):
Joran 的建议使用 Zoo 包的 fill-down 函数来节省打字:
What about just filling down afterwards? Somewhat ugly example (uses a loop, but hard to avoid with sequential dependencies like this):
Joran's suggestion saves typing using the zoo package's fill-down function: