salhin
salhin

Reputation: 2654

Change `character` to `numeric` in time series

Suppose I have the following data set.

structure(c("0.233632449583826", "0.252105485477729", "0.591295809149662", 
"0.0901324567177099", "-0.0423290120373304", "0.0363874105632916", 
"-0.136952003053153", "0.451355935617868", "-0.291897852322839", 
"0.287789031880016", "-2.1", "-1.4", "-2.6", "1.9", "-0.7", "1.4", 
"-0.6", "-1.3", "-1.4", "0"), .Dim = c(10L, 2L), .Dimnames = list(
    NULL, c("Return", "CI")), .Tsp = c(1985, 1985.75, 12), class = c("mts", 
"ts", "matrix"))

I am trying to calculate descriptive statistics and correlation between Return and CI using describe and cor. However, it gives the following error:

Error in describe(x) : non-numeric argument to 'describe'

It seems that the values in my data set are character not numeric. I tried as.numeric and data.matrix but still gives FALSE to is.numeric test.

Upvotes: 1

Views: 1310

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269824

If x is the object then convert it to numeric; however, that will cause the attributes to be dropped so add them back in:

xx <- as.numeric(x)
attributes(xx) <- attributes(x)

You might want to investigate how you wound up with this in the first place. There might be something else wrong too.

Upvotes: 5

Related Questions