Robert Kubrick
Robert Kubrick

Reputation: 8713

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"

Upvotes: 2

Views: 8711

Answers (2)

isomorphismes
isomorphismes

Reputation: 8403

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.

Upvotes: 2

Robert Kubrick
Robert Kubrick

Reputation: 8713

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.

Upvotes: 5

Related Questions