如何在不同周期之间为XTS填充和填充NA?
我有一个xts对象,我曾经用.period()打开'up'周期来生成第二个xts对象。然后我将两个xts对象组合回更快的时间段。如何设置cbind()以便更换NA(见下文)并填充最新值?
require(xts)
data(sample_matrix)
x <- as.xts(sample_matrix)
x.wk <- to.weekly(x)
x2 <- cbind(x, x.wk[ , 4])
print(first(x2, "2 weeks"))
>head(x2)
Open High Low Close x.Close
2007-01-02 50.03978 50.11778 49.95041 50.11778 NA
2007-01-03 50.23050 50.42188 50.23050 50.39767 NA
2007-01-04 50.42096 50.42096 50.26414 50.33236 NA
2007-01-05 50.37347 50.37347 50.22103 50.33459 NA
2007-01-06 50.24433 50.24433 50.11121 50.18112 NA
2007-01-07 50.13211 50.21561 49.99185 49.99185 49.99185
2007-01-08 50.03555 50.10363 49.96971 49.98806 NA
2007-01-09 49.99489 49.99489 49.80454 49.91333 NA
2007-01-10 49.91228 50.13053 49.91228 49.97246 NA
2007-01-11 49.88529 50.23910 49.88529 50.23910 NA
2007-01-12 50.21258 50.35980 50.17176 50.28519 NA
2007-01-13 50.32385 50.48000 50.32385 50.41286 NA
2007-01-14 50.46359 50.62395 50.46359 50.60145 50.60145
>head(x3) # this is what I want instead
Open High Low Close x.Close
2007-01-02 50.03978 50.11778 49.95041 50.11778 NA
2007-01-03 50.23050 50.42188 50.23050 50.39767 NA
2007-01-04 50.42096 50.42096 50.26414 50.33236 NA
2007-01-05 50.37347 50.37347 50.22103 50.33459 NA
2007-01-06 50.24433 50.24433 50.11121 50.18112 NA
2007-01-07 50.13211 50.21561 49.99185 49.99185 49.99185
2007-01-08 50.03555 50.10363 49.96971 49.98806 49.99185
2007-01-09 49.99489 49.99489 49.80454 49.91333 49.99185
2007-01-10 49.91228 50.13053 49.91228 49.97246 49.99185
2007-01-11 49.88529 50.23910 49.88529 50.23910 49.99185
2007-01-12 50.21258 50.35980 50.17176 50.28519 49.99185
2007-01-13 50.32385 50.48000 50.32385 50.41286 49.99185
2007-01-14 50.46359 50.62395 50.46359 50.60145 50.60145
谢谢!
没有找到相关结果
已邀请:
2 个回复
魄龟呸筹
。
绵扇寸访
另一种是程序化的方式: