How to overcome error:”attempt to set ‘colnames’ on an object with less than two dimension” in xts object

You probably want:

q <- data.frame(100,200,200,200,200,200)
colnames(q) <- colnames(SPY)
q <- xts(q, as.Date("2016-01-26"))
#            SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
# 2016-01-26      100      200     200       200        200          200

class(SPY)
# [1] "xts" "zoo"
class(q)
# [1] "xts" "zoo"

tail(rbind(SPY, q))
#            SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
# 2016-01-19   189.96   190.11  186.20    188.06  190196000       188.06
# 2016-01-20   185.03   187.50  181.02    185.65  280016900       185.65
# 2016-01-21   186.21   188.87  184.64    186.69  189174000       186.69
# 2016-01-22   189.78   190.76  188.88    190.52  163849600       190.52
# 2016-01-25   189.92   190.15  187.41    187.64  122676200       187.64
# 2016-01-26   100.00   200.00  200.00    200.00        200       200.00

Leave a Comment