按索引合并多个XTS对象

时间:2017-07-04 17:16:12

标签: r list merge xts

多个XTS对象。每个只包含一列(价格)。 物体可以具有不同的长度。我想按索引合并它们。 我可以将两个XTS对象与merge.xts合并。但我想合并多个 (使用一个功能,无需多次重复合并过程。)

所以我应用了推荐的方法here。有用。只有一个问题:它将所有数据合并到一个列中,而不是将每个XTS对象合并到其自己的列中。例如在可重现的示例中,期望的最终结果是XTSM具有3列。

如何实现?谢谢。

library('xts')

#Data
XTS1 <- structure(c(125.26, 125.14, 125.21, 125.26, 125.21, 125.26, 125.24, 125.22, 125.3, 125.32, 125.38, 125.38, 125.29, 125.26, 125.3, 125.25, 125.29, 125.37, 125.21, 125.3, 125.25, 125.24, 125.21, 125.24, 125.24, 125.26, 125.23, 125.23), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", .CLASS = "double", index = structure(c(1403537400, 1403538300, 1403539200, 1403540100, 1403541000, 1403541900, 1403542800, 1403543700, 1403544600, 1403545500, 1403546400, 1403547300, 1403548200, 1403549100, 1403550000, 1403550900, 1403551800, 1403552700, 1403553600, 1403554500, 1403555400, 1403556300, 1403557200, 1403560800, 1403561700, 1403562600, 1403563500, 1403564400), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(28L, 1L))
XTS2 <- structure(c(1867, 1867, 1868.5, 1868.75, 1868.75, 1868.75, 1868.25, 1867.25, 1867.5, 1867.75, 1868, 1868.25, 1868.25, 1868.75, 1868, 1868.5, 1869, 1869.75, 1868.75, 1868.5, 1868.25, 1867.5, 1867, 1866.75, 1866.75, 1867, 1867.5), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", .CLASS = "double", index = structure(c(1403537400, 1403538300, 1403539200, 1403540100, 1403541000, 1403541900, 1403542800, 1403543700, 1403544600, 1403545500, 1403546400, 1403547300, 1403548200, 1403549100, 1403550000, 1403550900, 1403551800, 1403552700, 1403553600, 1403555400, 1403556300, 1403557200, 1403560800, 1403561700, 1403562600, 1403563500, 1403564400), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(27L, 1L))
XTS3 <- structure(c(1867, 1867, 1868.5, 1868.75, 1868.75, 1868.75, 1868.25, 1867.25, 1867.5, 1867.75, 1868, 1868.25, 1868.25, 1868.75, 1868, 1868.5, 1869, 1869.75, 1868.75, 1868.5, 1868.25, 1867.5, 1867, 1866.75, 1866.75, 1867, 1867.5), class = c("xts", "zoo"), .indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"), .indexTZ = "", tzone = "", .CLASS = "double", index = structure(c(1403537400, 1403538300, 1403539200, 1403540100, 1403541000, 1403541900, 1403542800, 1403543700, 1403544600, 1403545500, 1403546400, 1403547300, 1403548200, 1403549100, 1403550000, 1403550900, 1403551800, 1403552700, 1403553600, 1403555400, 1403556300, 1403557200, 1403560800, 1403561700, 1403562600, 1403563500, 1403564400), tzone = "", tclass = c("POSIXct", "POSIXt")), .Dim = c(27L, 1L))

#Following code merges 2 xts objects into a 2 column xts
XTSM <- merge(XTS1,XTS2, all=TRUE)

#Following code merges multiple xts objects into a 1 column xts
XTSlist <- list(XTS1, XTS2, XTS3)

do.call.rbind <- function(lst) {
  while(length(lst) > 1) {
    idxlst <- seq(from=1, to=length(lst), by=2)
    lst <- lapply(idxlst, function(i) {
      if(i==length(lst)) { return(lst[[i]]) }
      return(rbind(lst[[i]], lst[[i+1]]))
    })
  }
  lst[[1]]
}

XTSM <- do.call.rbind (XTSlist)

1 个答案:

答案 0 :(得分:1)

约书亚在评论中回答了这个问题。为了回答Q而发布解决方案。就这么简单:

XTSM.T <- merge(XTS1,XTS2,XTS3, all=TRUE)

or

XTSM.F <- merge(XTS1,XTS2,XTS3, all=FALSE)