我有以下由股票价格组成的数据框列表。每个数据框可能具有不同的长度,并且并非所有日期都在每个数据框中。我想将列表转换为时间序列,第一列为日期,每一列为股票的价格,列名为股票的名称。如果在给定日期缺少价格,请采用之前的价格。
$`0P00012DY5`<br/>
last<br/>
2017-08-21 9.49504<br/>
2017-08-22 9.53553<br/>
2017-08-23 9.52643<br/>
2017-08-24 9.53486<br/>
2017-08-25 9.53390<br/>
2017-08-28 9.48788<br/>
2017-08-29 9.44059<br/>
2017-08-30 9.49893<br/>
$`0P0000KY8J`<br/>
last<br/>
2017-08-21 11.58276<br/>
2017-08-22 11.58278<br/>
2017-08-23 11.58275<br/>
2017-08-24 11.58263<br/>
2017-08-25 11.58260<br/>
2017-08-29 11.58250<br/>
2017-08-30 11.58246<br/>
2017-08-31 11.58237<br/>
所需的输出
date 0P00012DY5 0P0000KY8J<br/>
2017-08-21 9.49504 11.58276<br/>
2017-08-22 9.53553 11.58278<br/>
2017-08-23 9.52643 11.58275<br/>
2017-08-24 9.53486 11.58263<br/>
2017-08-25 9.53390 11.58260<br/>
2017-08-28 9.48788 11.58260<br/>
2017-08-29 9.44059 11.58250<br/>
2017-08-30 9.49893 11.58246<br/>
2017-08-31 9.49893 11.58237<br/>
列表的结构:
mod_ll <- structure(list(`0P00012DY5` = structure(list(last = c(9.49504,
9.53553, 9.52643, 9.53486, 9.5339, 9.48788, 9.44059, 9.49893,
9.542, 9.56865, 9.5417, 9.54301, 9.52942, 9.54246, 9.54653, 9.60182,
9.63157, 9.62566, 9.64084, 9.62746, 9.65814, 9.6672, 9.66387)), .Names = "last", row.names = c("2017-08-21",
"2017-08-22", "2017-08-23", "2017-08-24", "2017-08-25", "2017-08-28",
"2017-08-29", "2017-08-30", "2017-08-31", "2017-09-01", "2017-09-04",
"2017-09-05", "2017-09-06", "2017-09-07", "2017-09-08", "2017-09-11",
"2017-09-12", "2017-09-13", "2017-09-14", "2017-09-15", "2017-09-18",
"2017-09-19", "2017-09-20"), class = "data.frame"), `0P0000KY8J` = structure(list(
last = c(11.58276, 11.58278, 11.58275, 11.58263, 11.5826,
11.58254, 11.5825, 11.58246, 11.58237, 11.58231, 11.58223,
11.58219, 11.58218, 11.58206, 11.58202, 11.58197, 11.58193,
11.58189, 11.58178, 11.58174, 11.58173, 11.58166, 11.58163
)), .Names = "last", row.names = c("2017-08-21", "2017-08-22",
"2017-08-23", "2017-08-24", "2017-08-25", "2017-08-28", "2017-08-29",
"2017-08-30", "2017-08-31", "2017-09-01", "2017-09-04", "2017-09-05",
"2017-09-06", "2017-09-07", "2017-09-08", "2017-09-11", "2017-09-12",
"2017-09-13", "2017-09-14", "2017-09-15", "2017-09-18", "2017-09-19",
"2017-09-20"), class = "data.frame")), .Names = c("0P00012DY5",
"0P0000KY8J"))
答案 0 :(得分:2)
基于UI
的解决方案可以是使用tidyverse
组合列表中的所有data.frame。然后将dplyr::bind_rows
和Date
分成2列,并使用Value
以wide
格式更改数据。
最后,对于缺少的日期,请使用tidyr::spread
来填充最后一个可用值。
tidyr::fill
已编辑:已更新,其中包含OP提供的已修改数据的答案。提供library(tidyverse)
bind_rows(ll, .id="Name") %>%
separate(last, c("Date", "Value"), sep=" ") %>%
mutate(Date = as.Date(Date)) %>%
spread(Name, Value) %>%
fill(2:3)
# Date 0P0000KY8J 0P00012DY5
# 1 2017-08-21 11.58276 9.49504
# 2 2017-08-22 11.58278 9.53553
# 3 2017-08-23 11.58275 9.52643
# 4 2017-08-24 11.58263 9.53486
# 5 2017-08-25 11.58260 9.53390
# 6 2017-08-28 11.58260 9.48788
# 7 2017-08-29 11.58250 9.44059
# 8 2017-08-30 11.58246 9.49893
# 9 2017-08-31 11.58237 9.49893
作为Date
的行名。我们需要在合并数据帧之前将data.frames
移至列。可以将Date
与列表上的rownames_to_column
一起使用,将lapply
移到列中。
rownames
数据:
library(tidyverse)
bind_rows(lapply(mod_ll,rownames_to_column, var="Date"), .id="Name") %>%
mutate(Date = as.Date(Date)) %>%
spread(Name, last) %>%
fill(2:3)
# Date 0P0000KY8J 0P00012DY5
# 1 2017-08-21 11.58276 9.49504
# 2 2017-08-22 11.58278 9.53553
# 3 2017-08-23 11.58275 9.52643
# 4 2017-08-24 11.58263 9.53486
# 5 2017-08-25 11.58260 9.53390
# 6 2017-08-28 11.58254 9.48788
#
#....so on