英文:
How to merge multiple dataframes with different rows using dplyr
问题
以下是代码部分的翻译:
我在R中有以下数据框,它们都具有不同数量的行和不同的日期。
data1 <- structure(list(Date = structure(c(18628, 18629, 18630, 18631), class = "Date"),
Value1 = c(1, 2, 3, 4)), row.names = c(NA, -4L), class = c("tbl_df",
"tbl", "data.frame"))
data2 <- structure(list(Date = structure(c(18628, 18632, 18633), class = "Date"),
Value2 = c(1, 2, 3)), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"))
data3 <- structure(list(Date = structure(c(18626, 18629, 18633, 18634,
18635), class = "Date"), Value3 = c(1, 2, 3, 4, 5)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
我想将它们全部合并成一个数据框。通常情况下,我会使用full_join
,但这仅适用于两个数据框,如下所示:
library(tidyverse)
data <- full_join(data1, data2, by = 'Date') %>%
arrange(Date)
是否有一种简单的方法可以将超过2个数据框合并成一个数据框?
英文:
I have the following dataframes in R, which all have a different number of rows and different dates in them as well.
data1 <- structure(list(Date = structure(c(18628, 18629, 18630, 18631), class = "Date"),
Value1 = c(1, 2, 3, 4)), row.names = c(NA, -4L), class = c("tbl_df",
"tbl", "data.frame"))
data2 <- structure(list(Date = structure(c(18628, 18632, 18633), class = "Date"),
Value2 = c(1, 2, 3)), row.names = c(NA, -3L), class = c("tbl_df",
"tbl", "data.frame"))
data3 <- structure(list(Date = structure(c(18626, 18629, 18633, 18634,
18635), class = "Date"), Value3 = c(1, 2, 3, 4, 5)), row.names = c(NA,
-5L), class = c("tbl_df", "tbl", "data.frame"))
I would like to combine all three of them into one dataframe. Usually, I would use full_join
for this but that's only possible for two dataframes, like this
library(tidyverse)
data <- full_join(data1, data2, by = 'Date') %>%
arrange(Date)
Is there a simple way in which I can merge more than 2 dataframes into one dataframe?
答案1
得分: 2
将你的数据框收集到一个列表中并使用reduce
函数:
mget(ls(pattern = "^data")) %>%
reduce(full_join, by = "Date")
一个数据表:9行 × 4列
日期 值1 值2 值3
1 2021-01-01 1 1 NA
2 2021-01-02 2 NA 2
3 2021-01-03 3 NA NA
4 2021-01-04 4 NA NA
5 2021-01-05 NA 2 NA
6 2021-01-06 NA 3 3
7 2020-12-30 NA NA 1
8 2021-01-07 NA NA 4
9 2021-01-08 NA NA 5
<details>
<summary>英文:</summary>
Collect your data frames in a list and use `reduce`:
```r
mget(ls(pattern = "^data")) %>%
reduce(full_join, by = "Date")
# A tibble: 9 × 4
Date Value1 Value2 Value3
<date> <dbl> <dbl> <dbl>
1 2021-01-01 1 1 NA
2 2021-01-02 2 NA 2
3 2021-01-03 3 NA NA
4 2021-01-04 4 NA NA
5 2021-01-05 NA 2 NA
6 2021-01-06 NA 3 3
7 2020-12-30 NA NA 1
8 2021-01-07 NA NA 4
9 2021-01-08 NA NA 5
答案2
得分: 1
使用 base R
Reduce(function(...) merge(..., all = TRUE), mget(ls(pattern = "^data\\d+$")))
-输出
Date Value1 Value2 Value3
1 2020-12-30 NA NA 1
2 2021-01-01 1 1 NA
3 2021-01-02 2 NA 2
4 2021-01-03 3 NA NA
5 2021-01-04 4 NA NA
6 2021-01-05 NA 2 NA
7 2021-01-06 NA 3 3
8 2021-01-07 NA NA 4
9 2021-01-08 NA NA 5
或者使用 plyr::join_all
plyr::join_all(mget(ls(pattern = "^data\\d+$")), type = "full")
Date Value1 Value2 Value3
1 2021-01-01 1 1 NA
2 2021-01-02 2 NA 2
3 2021-01-03 3 NA NA
4 2021-01-04 4 NA NA
5 2021-01-05 NA 2 NA
6 2021-01-06 NA 3 3
7 2020-12-30 NA NA 1
8 2021-01-07 NA NA 4
9 2021-01-08 NA NA 5
英文:
Using base R
Reduce(function(...) merge(..., all = TRUE), mget(ls(pattern = "^data\\d+$")))
-output
Date Value1 Value2 Value3
1 2020-12-30 NA NA 1
2 2021-01-01 1 1 NA
3 2021-01-02 2 NA 2
4 2021-01-03 3 NA NA
5 2021-01-04 4 NA NA
6 2021-01-05 NA 2 NA
7 2021-01-06 NA 3 3
8 2021-01-07 NA NA 4
9 2021-01-08 NA NA 5
Or with plyr::join_all
plyr::join_all(mget(ls(pattern = "^data\\d+$")), type = "full")
Date Value1 Value2 Value3
1 2021-01-01 1 1 NA
2 2021-01-02 2 NA 2
3 2021-01-03 3 NA NA
4 2021-01-04 4 NA NA
5 2021-01-05 NA 2 NA
6 2021-01-06 NA 3 3
7 2020-12-30 NA NA 1
8 2021-01-07 NA NA 4
9 2021-01-08 NA NA 5
答案3
得分: 0
你可以很简单地在基本的 R 中完成它:
group_data <- rbind.data.frame(data1, data2, data3)
group_data
英文:
You can do it in base R quite simply:
group_data<-rbind.data.frame(data1,data2,data3)
group_data
答案4
得分: 0
full_join(data1, data2) %>%
full_join(., data3)
Joining, by = "Date"
# A tibble: 9 × 4
Date Value1 Value2 Value3
<date> <dbl> <dbl> <dbl>
1 2021-01-01 1 1 NA
2 2021-01-02 2 NA 2
3 2021-01-03 3 NA NA
4 2021-01-04 4 NA NA
5 2021-01-05 NA 2 NA
6 2021-01-06 NA 3 3
7 2020-12-30 NA NA 1
8 2021-01-07 NA NA 4
9 2021-01-08 NA NA 5
英文:
full_join(data1, data2) %>%
full_join(., data3)
Joining, by = "Date"
# A tibble: 9 × 4
Date Value1 Value2 Value3
<date> <dbl> <dbl> <dbl>
1 2021-01-01 1 1 NA
2 2021-01-02 2 NA 2
3 2021-01-03 3 NA NA
4 2021-01-04 4 NA NA
5 2021-01-05 NA 2 NA
6 2021-01-06 NA 3 3
7 2020-12-30 NA NA 1
8 2021-01-07 NA NA 4
9 2021-01-08 NA NA 5
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论