Search code examples
rxtsquantmodr-factor

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


Solution

  • 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.