合并 XTS 数据时重复值

发布于 2024-11-25 12:39:07 字数 994 浏览 4 评论 0原文

如果我的一个表有多个时间戳值,而另一个表只有一个,我可以重复多个值中单个项目的值吗?

例如:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

勿挽旧人 2024-12-02 12:39:07

那之后再填呢?有点丑陋的例子(使用循环,但很难避免像这样的顺序依赖):

mrg <- merge(XTS_A,XTS_B)
for(r in seq(nrow(mrg)) ) {
  if(is.na(mrg[r,1])) {
    mrg[r,1] <- mrg[r-1,1] 
  }
}

> mrg
                    XTS_A XTS_B
2011-01-01 16:00:00 "Bar" "A"  
2011-01-01 16:00:00 "Bar" "B"  
2011-01-01 16:00:00 "Bar" "C"  
2011-01-01 16:00:01 "Baz" "B"

Joran 的建议使用 Zoo 包的 fill-down 函数来节省打字:

mrg[,1] <- na.locf(as.vector(mrg$XTS_A))

What about just filling down afterwards? Somewhat ugly example (uses a loop, but hard to avoid with sequential dependencies like this):

mrg <- merge(XTS_A,XTS_B)
for(r in seq(nrow(mrg)) ) {
  if(is.na(mrg[r,1])) {
    mrg[r,1] <- mrg[r-1,1] 
  }
}

> mrg
                    XTS_A XTS_B
2011-01-01 16:00:00 "Bar" "A"  
2011-01-01 16:00:00 "Bar" "B"  
2011-01-01 16:00:00 "Bar" "C"  
2011-01-01 16:00:01 "Baz" "B"

Joran's suggestion saves typing using the zoo package's fill-down function:

mrg[,1] <- na.locf(as.vector(mrg$XTS_A))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文