简体   繁体   中英

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

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.

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