我想基于Time
加入所有三个文件。如果数据框没有该特定日期的值,我想添加NA
。总的来说,输出中应该有7列。
> head(Dax1,3)
Time Res Accum
1 2017-10-20 1174.60 172278.21
2 2017-10-18 -109.41 171103.61
3 2017-08-28 -2670.84 171213.02
> head(Dax2,3)
Time Res Accum
1 2017-10-23 1473.25 185076.53
2 2017-08-24 1001.50 183603.28
3 2017-07-31 -144.96 182601.79
> head(Dax3,3)
Time Res Accum
1 2017-11-07 -348.37 189 023.90
2 2017-10-26 398.16 189 372.27
3 2017-10-25 -80.19 188 974.10
我试过了join_all
,但似乎是以长格式出现的。我希望它们并排成7列:
> join_all(list, by = "Time",match = "all",type="full")
Time Res Accum
1 2017-10-20 1174.60 172278.21
2 2017-10-18 -109.41 171103.61
3 2017-08-28 -2670.84 171213.02
4 2017-10-23 1473.25 185076.53
5 2017-08-24 1001.50 183603.28
6 2017-07-31 -144.96 182601.79
7 2017-11-07 -348.37 189 023.90
8 2017-10-26 398.16 189 372.27
9 2017-10-25 -80.19 188 974.10
答案 0 :(得分:1)
我们可以重命名list
中数据集的列,但“时间”列除外,并应用join_all
res1 <- plyr::join_all(Map(function(x,y) {
names(x)[-1] <- paste0(names(x)[-1], y)
x}, lst, seq_along(lst)), by = "Time",match = "all",type="full")
或者可以使用tidyverse
library(purrr)
library(dplyr)
res2 <- lst %>%
map2(., seq_along(.), ~setNames(.x, c('Time', paste0(names(.x)[-1], .y)))) %>%
plyr::join_all(., by = 'Time', match = 'all', type = 'full')
identical(res1, res2)
#[1] TRUE