gpt4 book ai didi

r - 索引矩阵是否/应该与 xts 对象一起使用?

转载 作者:行者123 更新时间:2023-12-01 02:27:54 24 4
gpt4 key购买 nike

当我将索引矩阵与 xts 对象一起使用时,我得到了令人惊讶的结果。见下文。

如果我将 x 强制转换为矩阵(不出所料),它会按预期工作。

感谢您的帮助。

> data(sample_matrix)
> x<- as.xts(sample_matrix)
> i<- matrix(1:4, 2, 2, byrow=T )
> i
[,1] [,2]
[1,] 1 2
[2,] 3 4
> head(x)
Open High Low Close
2007-01-02 50.03978 50.11778 49.95041 50.11778
2007-01-03 50.23050 50.42188 50.23050 50.39767
2007-01-04 50.42096 50.42096 50.26414 50.33236
2007-01-05 50.37347 50.37347 50.22103 50.33459
2007-01-06 50.24433 50.24433 50.11121 50.18112
2007-01-07 50.13211 50.21561 49.99185 49.99185
> x[i]<- NA
> head(x)
Open High Low Close
2007-01-02 NA 50.11778 49.95041 50.11778
2007-01-03 NA 50.42188 50.23050 50.39767
2007-01-04 NA 50.42096 50.26414 50.33236
2007-01-05 NA 50.37347 50.22103 50.33459
2007-01-06 50.24433 50.24433 50.11121 50.18112
2007-01-07 50.13211 50.21561 49.99185 49.99185

最佳答案

我有一个解决方法。代替

x[i]<- NA

使用核心数据:
> coredata(x)[i]<- NA
> head(x)
Open High Low Close
2007-01-02 50.03978 NA 49.95041 50.11778
2007-01-03 50.23050 50.42188 50.23050 50.39767
2007-01-04 50.42096 50.42096 50.26414 NA
2007-01-05 50.37347 50.37347 50.22103 50.33459
2007-01-06 50.24433 50.24433 50.11121 50.18112
2007-01-07 50.13211 50.21561 49.99185 49.99185

尽管在我(初学者)看来,xts 在原始代码中仍然做错了事情。

关于r - 索引矩阵是否/应该与 xts 对象一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14971612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com