合并/ Vlookup 2个不同尺寸的xts对象

时间:2019-06-14 15:01:21

标签: r xts cbind quantstrat

我正在尝试合并2个xts对象,其中第二个对象需要在第一个对象中填充新列-如果缺少值,请填充0

基本上,我有这两个xts对象

str(x1)
An ‘xts’ object on 1999-01-01/2019-06-14 containing:
  Data: num [1:5336, 1:14] 0.611 0.614 0.62 0.623 0.629 ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr [1:14] "Open" "High" "Low" "Close" ...
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
 NULL
> str(x2)
An ‘xts’ object on 1999-03-25/2019-04-25 containing:
  Data: chr [1:202, 1] "enter_long" "exit_long" "enter_long" "exit_long" "enter_long" "exit_long" "enter_long" "exit_long" ...
 - attr(*, "dimnames")=List of 2
  ..$ : NULL
  ..$ : chr "Rule"
  Indexed by objects of class: [Date] TZ: UTC
  xts Attributes:  
 NULL

我的x1对象看起来像这样

head(x1)
             Open   High    Low  Close  N     MA1 MA2 MA3 MA3_curve buy_signal exit_buysignal sell_signal exit_sellsignal pos
1999-01-01 0.6114 0.6138 0.6098 0.6103 NA 0.00000   0   0        NA          0              0           0               0   0
1999-01-04 0.6137 0.6201 0.6112 0.6182 NA 0.00000   0   0        NA          0              0           0               0   0
1999-01-05 0.6198 0.6255 0.6193 0.6224 NA 0.00000   0   0        NA          0              0           0               0   0
1999-01-06 0.6227 0.6305 0.6192 0.6283 NA 0.00000   0   0        NA          0              0           0               0   0
1999-01-07 0.6288 0.6354 0.6275 0.6348 NA 0.62280   0   0        NA          0              0           0               0   0
1999-01-08 0.6347 0.6384 0.6313 0.6339 NA 0.62752   0   0        NA          0              0           0               0   0

第二个x2对象看起来像这样

           Rule        
1999-03-25 "enter_long"
1999-03-30 "exit_long" 
1999-04-14 "enter_long"
1999-05-31 "exit_long" 
1999-06-10 "enter_long"
1999-06-18 "exit_long" 

当我使用

i <- cbind.xts(x1,x2, fill = "0")

它用0正确填充丢失的值,但用 NA 填充现有值 这是错误消息

Warning message:
In merge.xts(..., all = all, fill = fill, suffixes = suffixes) :
  NAs introduced by coercion

我已经尝试了所有发现的东西 -merge.xts / cbind.xts -从dplyr查找 -....

感谢您的帮助

0 个答案:

没有答案