简体   繁体   中英

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. I've tried using data.matrix() but the results are still in character format:

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.

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). The rest being some sample data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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