如何使用dplyr合并具有不同行的多个数据框。

huangapple go评论72阅读模式
英文:

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 &lt;- structure(list(Date = structure(c(18628, 18629, 18630, 18631), class = &quot;Date&quot;), 
    Value1 = c(1, 2, 3, 4)), row.names = c(NA, -4L), class = c(&quot;tbl_df&quot;, 
&quot;tbl&quot;, &quot;data.frame&quot;))

data2 &lt;- structure(list(Date = structure(c(18628, 18632, 18633), class = &quot;Date&quot;), 
    Value2 = c(1, 2, 3)), row.names = c(NA, -3L), class = c(&quot;tbl_df&quot;, 
&quot;tbl&quot;, &quot;data.frame&quot;))

data3 &lt;- structure(list(Date = structure(c(18626, 18629, 18633, 18634, 
18635), class = &quot;Date&quot;), Value3 = c(1, 2, 3, 4, 5)), row.names = c(NA, 
-5L), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;))

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 &lt;- full_join(data1, data2, by = &#39;Date&#39;) %&gt;% 
  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 = &quot;^data&quot;)) %&gt;% 
  reduce(full_join, by = &quot;Date&quot;)

# A tibble: 9 &#215; 4
  Date       Value1 Value2 Value3
  &lt;date&gt;      &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;
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 = &quot;^data\\d+$&quot;)))

-输出

        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 = &quot;^data\\d+$&quot;)), type = &quot;full&quot;)
        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 = &quot;^data\\d+$&quot;)))

-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 = &quot;^data\\d+$&quot;)), type = &quot;full&quot;)
        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&lt;-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) %&gt;% 
  full_join(., data3)
Joining, by = &quot;Date&quot;
# A tibble: 9 &#215; 4
  Date       Value1 Value2 Value3
  &lt;date&gt;      &lt;dbl&gt;  &lt;dbl&gt;  &lt;dbl&gt;
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

huangapple
  • 本文由 发表于 2023年3月15日 19:55:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744370.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定