【发布时间】:2020-12-24 19:02:12
【问题描述】:
您好,我在不同股票的数据框中有来自 yahoo 的数据(列符号)。 我想创建每只股票 1 行的数据框(tibble),其中的一列将包含 嵌套股票数据作为 xts 对象。添加了结果图片和可重复的示例。任何帮助表示赞赏
library(purrr)
library(tidyverse)
library(tidyr)
df<-structure(list(symbol = c("AAPL", "AAPL", "AAPL", "AAPL", "AAPL",
"AMZN", "AMZN", "AMZN", "AMZN", "AMZN", "MSFT", "MSFT", "MSFT",
"MSFT", "MSFT"), date = structure(c(18295, 16700, 17571, 18305,
18086, 17834, 17696, 17438, 16850, 18016, 18376, 17935, 18085,
17626, 17724), class = "Date"), adjusted = c(76.636299, 26.198639,
37.847511, 80.852463, 49.627182, 1530.420044, 1723.859985, 961.349976,
534.900024, 1926.52002, 173.645462, 103.308533, 134.968887, 89.195686,
101.034645)), row.names = c(NA, -15L), class = c("tbl_df", "tbl",
"data.frame"))
df%>%group_by(symbol)%>%
nest()%>%
mutate(xts_obj=map(data,~as.xts(order_by=.$date)))
【问题讨论】: