将时间序列数据合并到一个数据框中
Combining time series data into a single data frame
我有多个这样的数据框:
> head(Standard.df)
Count.S Date Month Week Year
552 15 2008-01-01 2008-01-01 2007-12-31 2008-01-01
594 11 2008-01-02 2008-01-01 2007-12-31 2008-01-01
1049 10 2008-01-03 2008-01-01 2007-12-31 2008-01-01
511 12 2008-01-04 2008-01-01 2007-12-31 2008-01-01
717 10 2008-01-06 2008-01-01 2007-12-31 2008-01-01
1744 3 2008-01-07 2008-01-01 2008-01-07 2008-01-01
> head(Guardian.df)
Count.G Date Month Week Year
2624 7 2006-01-02 2006-01-01 2006-01-02 2006-01-01
409 13 2006-01-03 2006-01-01 2006-01-02 2006-01-01
93 13 2006-01-04 2006-01-01 2006-01-02 2006-01-01
999 20 2006-01-05 2006-01-01 2006-01-02 2006-01-01
1387 19 2006-01-06 2006-01-01 2006-01-02 2006-01-01
2652 4 2006-01-07 2006-01-01 2006-01-02 2006-01-01
2652 4 2006-01-07 2006-01-01 2006-01-02 2006-01-01
> head(Welt.df)
Count.W Date Month Week Year
2506 9 2006-01-02 2006-01-01 2006-01-02 2006-01-01
384 12 2006-01-03 2006-01-01 2006-01-02 2006-01-01
87 15 2006-01-04 2006-01-01 2006-01-02 2006-01-01
947 6 2006-01-05 2006-01-01 2006-01-02 2006-01-01
1313 19 2006-01-06 2006-01-01 2006-01-02 2006-01-01
2532 16 2006-01-07 2006-01-01 2006-01-02 2006-01-01
表示时间的向量在所有数据帧中的长度都不同(有的是10年,有的是8年,等等)。理想情况下,我想将所有数据帧中的 Count
向量合并为一个,并以最长时间向量作为起点,如果其他数据帧中没有相应的日期 - 填写 NA
s
所以,像这样:
> head(Full.df)
Count.G Count.W Count.S Date Month Week Year
x x 15 2008-01-01 2008-01-01 2007-12-31 2008-01-01
x x 11 2008-01-02 2008-01-01 2007-12-31 2008-01-01
x x 10 2008-01-03 2008-01-01 2007-12-31 2008-01-01
x x 12 2008-01-04 2008-01-01 2007-12-31 2008-01-01
x x 10 2008-01-06 2008-01-01 2007-12-31 2008-01-01
x x 3 2008-01-07 2008-01-01 2008-01-07 2008-01-01
这可以在 R 中实现吗?
你可以试试
Reduce(function(...) merge(..., by=c('Date', 'Month', 'Week', 'Year'),
all=TRUE), list(Standard.df, Guardian.df, Welt.df))
我有多个这样的数据框:
> head(Standard.df)
Count.S Date Month Week Year
552 15 2008-01-01 2008-01-01 2007-12-31 2008-01-01
594 11 2008-01-02 2008-01-01 2007-12-31 2008-01-01
1049 10 2008-01-03 2008-01-01 2007-12-31 2008-01-01
511 12 2008-01-04 2008-01-01 2007-12-31 2008-01-01
717 10 2008-01-06 2008-01-01 2007-12-31 2008-01-01
1744 3 2008-01-07 2008-01-01 2008-01-07 2008-01-01
> head(Guardian.df)
Count.G Date Month Week Year
2624 7 2006-01-02 2006-01-01 2006-01-02 2006-01-01
409 13 2006-01-03 2006-01-01 2006-01-02 2006-01-01
93 13 2006-01-04 2006-01-01 2006-01-02 2006-01-01
999 20 2006-01-05 2006-01-01 2006-01-02 2006-01-01
1387 19 2006-01-06 2006-01-01 2006-01-02 2006-01-01
2652 4 2006-01-07 2006-01-01 2006-01-02 2006-01-01
2652 4 2006-01-07 2006-01-01 2006-01-02 2006-01-01
> head(Welt.df)
Count.W Date Month Week Year
2506 9 2006-01-02 2006-01-01 2006-01-02 2006-01-01
384 12 2006-01-03 2006-01-01 2006-01-02 2006-01-01
87 15 2006-01-04 2006-01-01 2006-01-02 2006-01-01
947 6 2006-01-05 2006-01-01 2006-01-02 2006-01-01
1313 19 2006-01-06 2006-01-01 2006-01-02 2006-01-01
2532 16 2006-01-07 2006-01-01 2006-01-02 2006-01-01
表示时间的向量在所有数据帧中的长度都不同(有的是10年,有的是8年,等等)。理想情况下,我想将所有数据帧中的 Count
向量合并为一个,并以最长时间向量作为起点,如果其他数据帧中没有相应的日期 - 填写 NA
s
所以,像这样:
> head(Full.df)
Count.G Count.W Count.S Date Month Week Year
x x 15 2008-01-01 2008-01-01 2007-12-31 2008-01-01
x x 11 2008-01-02 2008-01-01 2007-12-31 2008-01-01
x x 10 2008-01-03 2008-01-01 2007-12-31 2008-01-01
x x 12 2008-01-04 2008-01-01 2007-12-31 2008-01-01
x x 10 2008-01-06 2008-01-01 2007-12-31 2008-01-01
x x 3 2008-01-07 2008-01-01 2008-01-07 2008-01-01
这可以在 R 中实现吗?
你可以试试
Reduce(function(...) merge(..., by=c('Date', 'Month', 'Week', 'Year'),
all=TRUE), list(Standard.df, Guardian.df, Welt.df))