Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a factor column to quantmod/xts

what am i doing wrong here?

library(quantmod)
getSymbols('^GSPC')
b <- tail(GSPC, 20) #for brevity
is.factor(factor(Cl(b), labels=c('A')))
> TRUE
b$f <- factor(Cl(b), labels=c('A'))
is.factor(b$f)
[1] FALSE

I would like a column in my xts/quantmod object to be a factor. I have no idea why it is not working.

Thanks

like image 218
dizzy Avatar asked Sep 13 '26 08:09

dizzy


1 Answers

It doesn't work because xts/zoo objects are a matrix with an index attribute and you can't mix types in a matrix. GSPC contains numeric data, so you can only add numeric columns.

like image 83
Joshua Ulrich Avatar answered Sep 15 '26 23:09

Joshua Ulrich