R is a software environment as well as a programming language used for graphics reporting and representation, and statistical analysis. R is an open-source and free language that interprets the code and makes coding simpler for programmers and developers. When working with R, you may encounter the error “attempt to set ‘colnames’ on an object with less than two dimensions”.
How the error pops up
When you attempt to add a new row for open price at the end of the data frame ‘SPY’ that is produced using package quantmod, you get the following error warning:
Error in `colnames<-`(`*tmp*`, value = c("SPY.Open", "SPY.High", "SPY.Low", :
attempt to set 'colnames' on an object with less than two dimensions # creating the column names
This error appears when you use the following code to rbind new row:
# rm(list = ls()) # generally considered as bad manner in an MWE
require(quantmod)
options(scipen=999)
spy <- getSymbols(("SPY") , src = 'yahoo', from = '2016-01-01', auto.assign = T)
spy<-cbind(SPY)
tail(SPY)
SPY.Open SPY.High SPY.Low SPY.Close SPY.Volume SPY.Adjusted
2016-01-14 189.55 193.26 187.66 191.93 240795600 191.93
2016-01-15 186.77 188.76 185.52 187.81 324846400 187.81
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
When you manually try to add a new row into the dataset spy for creating a new xts object to later use the function rbind, you typed the following lines:
q <- c("2016-01-25",100,200,200,200,200,200) # creating the data
colnames(q) <- colnames(SPY) # creating column names as in SPY
All of these code scripts return an error to make you think about what could be the best solution to handle it. Well, check out the next section to know it
The Solution to Handle the Error “attempt to set ‘colnames’ on an object with less than two dimensions”
The error appears when setting ‘colnames’ on an object that has dimensions of more than two. To fix the error efficiently, we bring an amazing solution. Take a look at it
Solution
The code you are trying to execute is somehow like this:
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
Conclusion
We discussed a short and simple solution to help you fix the error “attempt to set ‘colnames’ on an object with less than two dimensions”. It is easy to implement as well as really effective.
I hope you find it interesting and helpful!
If you need further assistance, feel free to write to us in the comment box below and we will get back to you with tips and solutions to address your issue.