简体   繁体   English

R:如何将xts对象转换为数值矩阵?

[英]R: How to convert an xts object to a numeric matrix?

I need to convert an xts matrix to a numeric matrix or a data.frame with numeric columns, whatever works. 我需要将xts矩阵转换为数字矩阵或具有数字列的data.frame,无论如何工作。 I've tried using data.matrix() but the results are still in character format: 我尝试使用data.matrix(),但结果仍为字符格式:

class(myxts)
[1] "xts" "zoo"
class(myxts[, "MyNumericColumn"]
[1] "xts" "zoo"

myxts.num = data.matrix(myxts)
class(myxts.num[, "MyNumericColumn"]
[1] "character"

This does the trick although I'm not sure what the performance penalty is: 尽管我不确定性能会降低多少,但这可以解决问题:

mynumeric.matrix = data.matrix(as.data.frame(myxts))

The call to as.data.frame converts all strings to numeric values, then data.matrix() returns the matrix changing all remaining strings to NAs. 对as.data.frame的调用将所有字符串转换为数值,然后data.matrix()返回将所有剩余字符串更改为NA的矩阵。

I'm not sure if this is what you're asking but I did 我不确定这是否是您要问的,但我确实做到了

require(YieldCurve)
data(FedYieldCurve)
august <- FedYieldCuve['2011-08']
unclass(august)[1,]

The unclass being the function that does what I think you want to do (remove the xts -specific attr 's and only use the numbers). unclass是执行我想做的功能的函数(删除xts -specific attr ,仅使用数字)。 The rest being some sample data. 其余的是一些样本数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM