2014-02-20 33 views
1

我需要結合日內OHLC數據的兩個xts對象a和b。一個較早開始,另一個結束,它們重疊。我需要的是一個新的xts對象c,它從a的第一個時期開始,到b的最後一個時期結束。如果一段時間a和b都提供OHCLV a優先,否則取a和b中的任意一個。結合來自不同來源的xts OHLC

例如:

> a 
##       a.Open  a.High  a.Low  a.Close  a.Volume 
## 2014-02-18 09:55:00  184.14  184.46  184.07  184.11  5712100 
## 2014-02-18 10:55:00  184.12  184.28  183.65  184.17  13912700 
## 2014-02-18 11:55:00  184.17  184.49  184.12  184.38  7251000 
## 2014-02-18 12:55:00  184.38  184.45  184.18  184.26  5521100 
## 2014-02-18 13:55:00  184.26  184.40  184.11  184.15  4700300 
## 2014-02-18 15:55:00  184.40  184.47  184.26  184.37  10324400 
## 2014-02-18 16:05:00  184.38  184.38  184.20  184.24  20452900 
## 2014-02-19 09:55:00  183.69  184.29  183.69  184.24  5445800 
## 2014-02-19 10:55:00  184.25  184.95  184.16  184.74  18007800 

> b 
##        b.Open   b.High   b.Low   b.Close  b.Volume 
## 2014-02-14 15:55:00   183.96   184.06  183.15   183.99   18607278 
## 2014-02-14 16:05:00   183.99   184.00  183.98   183.99   52504 
## 2014-02-18 09:55:00   184.16   184.28  184.11   184.15   3932026 
## 2014-02-18 10:55:00   184.14   184.49  184.12   184.40   7570591 
## 2014-02-18 11:55:00   184.40   184.45  184.18   184.35   5201513 
## 2014-02-18 12:55:00   184.35   184.40  184.11   184.16   4870456 
## 2014-02-18 13:55:00   184.15   184.48  184.13   184.41   5824933 
## 2014-02-18 14:55:00   184.41   184.47  184.26   184.42   7617463 
## 2014-02-18 15:55:00   184.43   184.43  183.98   184.29   9575112 
## 2014-02-18 16:00:00   184.27   184.27  184.26   184.27    1990 

應導致:

> c 
##      c.Open  c.High  c.Low  c.Close  c.Volume 
## 2014-02-14 15:55:00  183.96  184.06  183.15  183.99  18607278 
## 2014-02-14 16:05:00  183.99  184.00  183.98  183.99   52504     
## 2014-02-18 09:55:00  184.14  184.46  184.07  184.11  5712100 
## 2014-02-18 10:55:00  184.12  184.28  183.65  184.17  13912700 
## 2014-02-18 11:55:00  184.17  184.49  184.12  184.38  7251000 
## 2014-02-18 12:55:00  184.38  184.45  184.18  184.26  5521100 
## 2014-02-18 13:55:00  184.26  184.40  184.11  184.15  4700300 
## 2014-02-18 14:55:00   184.41   184.47  184.26   184.42   7617463 
## 2014-02-18 15:55:00  184.40  184.47  184.26  184.37  10324400 
## 2014-02-18 16:05:00  184.38  184.38  184.20  184.24  20452900 
## 2014-02-19 09:55:00  183.69  184.29  183.69  184.24  5445800 
## 2014-02-19 10:55:00  184.25  184.95  184.16  184.74  18007800 

注意,爲14時55分00秒期間在一個缺少的值填充有從b中的值。

回答

0

這不是一個真正的合併問題。你真的想rbind所有的值從a,與那些從ba沒有一個給定的時間觀察。

a <- structure(c(184.14, 184.12, 184.17, 184.38, 184.26, 184.4, 184.38, 
183.69, 184.25, 184.46, 184.28, 184.49, 184.45, 184.4, 184.47, 184.38, 
184.29, 184.95, 184.07, 183.65, 184.12, 184.18, 184.11, 184.26, 184.2, 
183.69, 184.16, 184.11, 184.17, 184.38, 184.26, 184.15, 184.37, 184.24, 
184.24, 184.74, 5712100, 13912700, 7251000, 5521100, 4700300, 10324400, 
20452900, 5445800, 18007800), .Dim = c(9L, 5L), .Dimnames = list(NULL, 
c("a.Open", "a.High", "a.Low", "a.Close", "a.Volume")), 
index = structure(c(1392738900, 1392742500, 1392746100, 1392749700, 
1392753300, 1392760500, 1392761100, 1392825300, 1392828900), tzone = "", 
tclass = c("POSIXct", "POSIXt")), class = c("xts", "zoo"), 
.indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"), 
.indexTZ = "", tzone = "") 

b <- structure(c(183.96, 183.99, 184.16, 184.14, 184.4, 184.35, 184.15, 
184.41, 184.43, 184.27, 184.06, 184, 184.28, 184.49, 184.45, 184.4, 184.48, 
184.47, 184.43, 184.27, 183.15, 183.98, 184.11, 184.12, 184.18, 184.11, 
184.13, 184.26, 183.98, 184.26, 183.99, 183.99, 184.15, 184.4, 184.35, 
184.16, 184.41, 184.42, 184.29, 184.27, 18607278, 52504, 3932026, 7570591, 
5201513, 4870456, 5824933, 7617463, 9575112, 1990), .Dim = c(10L, 5L), 
.Dimnames = list(NULL, c("b.Open", "b.High", "b.Low", "b.Close", "b.Volume")), 
index = structure(c(1392414900, 1392415500, 1392738900, 1392742500, 
1392746100, 1392749700, 1392753300, 1392756900, 1392760500, 1392760800), 
tzone = "", tclass = c("POSIXct", "POSIXt")), class = c("xts", "zoo"), 
.indexCLASS = c("POSIXct", "POSIXt"), tclass = c("POSIXct", "POSIXt"), 
.indexTZ = "", tzone = "") 

我們可以簡單的子集b到只有不在a意見,然後調用rbind

> rbind(a, b[!(index(b) %in% index(a))]) 
        a.Open a.High a.Low a.Close a.Volume 
2014-02-14 15:55:00 183.96 184.06 183.15 183.99 18607278 
2014-02-14 16:05:00 183.99 184.00 183.98 183.99 52504 
2014-02-18 09:55:00 184.14 184.46 184.07 184.11 5712100 
2014-02-18 10:55:00 184.12 184.28 183.65 184.17 13912700 
2014-02-18 11:55:00 184.17 184.49 184.12 184.38 7251000 
2014-02-18 12:55:00 184.38 184.45 184.18 184.26 5521100 
2014-02-18 13:55:00 184.26 184.40 184.11 184.15 4700300 
2014-02-18 14:55:00 184.41 184.47 184.26 184.42 7617463 
2014-02-18 15:55:00 184.40 184.47 184.26 184.37 10324400 
2014-02-18 16:00:00 184.27 184.27 184.26 184.27  1990 
2014-02-18 16:05:00 184.38 184.38 184.20 184.24 20452900 
2014-02-19 09:55:00 183.69 184.29 183.69 184.24 5445800 
2014-02-19 10:55:00 184.25 184.95 184.16 184.74 18007800