我有以下内容,它们为每个证券的历史价格创建了三个嵌套列表,例如 福特的价格为$ F。
"${f##+([0-9])}"
我想得到一个数据框...
所以输出看起来像
请注意,我不能在此处使用library(tseries)
tickers <- c('F','MSFT','GOOG')
end = Sys.Date()-1
Price <- list()
for(i in tickers){
Price[[i]] <- get.hist.quote(i, end = Sys.Date()-1,
quote = c("Open", "High", "Low", "Close", "Adjusted", "Volume"))
}
等,但不必一定要首先使用列表。
答案 0 :(得分:0)
由于您需要一个组合的数据框,因此可以使用zoo
将fortify.zoo
对象转换为数据框,并使用imap
添加一个新的列表名称列。
library(purrr)
Result <- imap_dfr(Price, ~.x %>% zoo::fortify.zoo() %>% mutate(Symbol= .y))
head(Result)
# Index Open High Low Close Adjusted Volume Symbol
#1 1991-01-02 4.812811 4.881240 4.630335 4.790002 1.689240 3109100 F
#2 1991-01-03 4.767192 4.858430 4.744383 4.744383 1.673152 3173400 F
#3 1991-01-04 4.767192 4.881240 4.744383 4.835621 1.705328 3211700 F
#4 1991-01-07 4.790002 4.790002 4.653145 4.653145 1.640976 3373900 F
#5 1991-01-08 4.698764 4.721573 4.630335 4.653145 1.640976 3045600 F
#6 1991-01-09 4.744383 4.812811 4.584716 4.584716 1.616845 5636200 F
tail(Result)
# Index Open High Low Close Adjusted Volume Symbol
#18902 2020-07-17 1521.62 1523.440 1498.420 1515.55 1515.55 1456700 GOOG
#18903 2020-07-20 1515.26 1570.290 1503.600 1565.72 1565.72 1557300 GOOG
#18904 2020-07-21 1586.99 1586.990 1554.280 1558.42 1558.42 1218600 GOOG
#18905 2020-07-22 1560.50 1570.000 1546.100 1568.49 1568.49 932000 GOOG
#18906 2020-07-23 1566.97 1571.870 1507.392 1515.68 1515.68 1627600 GOOG
#18907 2020-07-24 1498.93 1517.636 1488.400 1511.87 1511.87 1544000 GOOG