Merging multiple XTS objects by index
有多个XTS对象。每个仅包含一列(价格)。
物体可以具有不同的长度。我想按索引合并它们。
我可以用
(具有一项功能,无需多次重复合并过程)。
因此,我采用了这里推荐的方法。有用。只是一个问题:它将所有数据合并到一列中,而不是将每个要合并的XTS对象放入其自己的列中。例如在可重现的示例中,所需的最终结果是XTSM具有3列。
如何实现?谢谢。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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) |
约书亚(Joshua)在评论中回答问题。为解答问题而发布解决方案。就这么简单:
1 2 3 4 5 | XTSM.T <- merge(XTS1,XTS2,XTS3, all=TRUE) or XTSM.F <- merge(XTS1,XTS2,XTS3, all=FALSE) |