R 如何克服错误:“;尝试设置';colnames';在小于二维的物体上“;在xts对象中
我想在使用quantmod包生成的“SPY”数据帧的末尾添加一个开价作为新行,我使用了以下代码来rbind新行,但我得到了一个错误R 如何克服错误:“;尝试设置';colnames';在小于二维的物体上“;在xts对象中,r,xts,R,Xts,我想在使用quantmod包生成的“SPY”数据帧的末尾添加一个开价作为新行,我使用了以下代码来rbind新行,但我得到了一个错误 # 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 =
# 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
但我有一个错误:
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
`colnames中出现错误,您可能需要:
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
qq
是一个向量。没有列名称的向量。你可能想要q Hello@Pascal,我使用了你的建议,而不是使用rbind,但我得到了这个错误:请参阅下面的答案。
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