如何使用dplyr合并具有不同行的多个 Dataframe

ryevplcw  于 2023-03-20  发布在  其他
关注(0)|答案(4)|浏览(134)

我在R中有以下 Dataframe ,它们都有不同的行数和不同的日期。

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"))

我想将这三个 Dataframe 合并到一个 Dataframe 中。通常,我会使用full_join,但这只适用于两个 Dataframe ,如下所示

library(tidyverse)

data <- full_join(data1, data2, by = 'Date') %>% 
  arrange(Date)

有没有一种简单的方法可以将两个以上的 Dataframe 合并成一个 Dataframe ?

goqiplq2

goqiplq21#

在列表中收集 Dataframe 并使用reduce

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
rqqzpn5f

rqqzpn5f2#

使用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
iibxawm4

iibxawm43#

你可以很简单地用R做:

group_data<-rbind.data.frame(data1,data2,data3)
group_data
brc7rcf0

brc7rcf04#

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

相关问题