【发布时间】:2016-05-02 19:14:54
【问题描述】:
我想在使用 quantmod 包生成的“SPY”数据框的末尾添加一个开盘价作为新行,我使用以下代码来绑定新行,但出现错误
# 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
我想手动将新行插入到 spy 数据集中,所以我尝试创建一个新的 xts 对象,而不是使用 rbind 函数,但在这些行之后出现错误:
q <- c("2016-01-25",100,200,200,200,200,200) # creating the data
colnames(q) <- colnames(SPY) # creating column names as in SPY
但我得到了一个错误:
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
如何在数据框顶部添加这个手工制作的行?
【问题讨论】:
-
q是一个向量。没有列名的向量。你可能想要q <- data.frame(100,200,200,200,200,200),但要小心q和SPY的class。 -
你好@Pascal,我使用了你的建议,而不是使用 rbind,但我收到了这个错误:s
-
在下面查看我的答案。