问题描述
我现在正在为 xts 苦苦挣扎...我有一个 XTS 对象,我正在尝试用新的集合替换特定日期的数据,但我一直遇到替换长度的问题,即使我确定它们是相同的,没有丢失的数据.有什么想法吗?
I'm struggling with xts right now... I have an XTS object, and I am trying to replace a specific date's data with a new set, but I keep running into issues of replacement length, even though I am certain they are the same and no missing data. Any ideas?
我做了一个虚拟示例来演示我的问题:
I made a dummy example to demonstrate my issues:
R> test1 = xts(cbind(trade_level = 1, t2 = 3),order.by=as.Date("1991-01-02"))
R> test2 = xts(cbind(trade_level = 5, t2 = .053),order.by=as.Date("1991-01-02"))
R> first_day = "1991-01-02"
R> test1[first_day] = test2
Warning message:
In NextMethod(.Generic) :
number of items to replace is not a multiple of replacement length
R> sessionInfo()
R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] reshape2_1.2.2 xts_0.9-7 zoo_1.7-10 ggplot2_0.9.3.1
loaded via a namespace (and not attached):
[1] colorspace_1.2-4 dichromat_2.0-0 digest_0.6.4 grid_3.0.1
[5] gtable_0.1.2 labeling_0.2 lattice_0.20-23 MASS_7.3-29
[9] munsell_0.4.2 plyr_1.8 proto_0.3-10 RColorBrewer_1.0-5
[13] scales_0.2.3 stringr_0.6.2 tools_3.0.1
推荐答案
这是警告,而不是错误.它会提示您出了什么问题……您需要添加一个逗号来指定要替换所有列.
That's a warning, not an error. And it gives you a hint about what's wrong... you need to add a comma to specify that you want to replace all columns.
test1 = xts(cbind(trade_level = 1, t2 = 3),order.by=as.Date("1991-01-02"))
test2 = xts(cbind(trade_level = 5, t2 = .053),order.by=as.Date("1991-01-02"))
first_day = "1991-01-02"
test1[first_day,] = test2
但不要怪 xts,这就是矩阵类的工作原理:
But don't blame xts, this is how the matrix class works:
test1 = xts(cbind(trade_level = 1, t2 = 3),order.by=as.Date("1991-01-02"))
test2 = xts(cbind(trade_level = 5, t2 = .053),order.by=as.Date("1991-01-02"))
m1 <- as.matrix(test1)
m2 <- as.matrix(test2)
m1[1] <- m2 # warning
m1[1,] <- m2 # works
这篇关于XTS 替换错误 NextMethod(.Generic):要替换的项目数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!