youraz
youraz

Reputation: 483

How to merge all data frames in the global environment respectively?

I've got a bunch of dataframes and want to merge all of them. But the point here is it should be sorted from master1 to master11 and the number of df isn't constant. The problem starts after master10 and so on.

DF_obj <- lapply(ls(pattern = ".*master"), get)
df <- DF_obj %>% reduce(left_join, by = "commonvar")

The code below is an example to explain it.

master1 <- data.frame(x = LETTERS[1:10], y1 = sample(1:100,10))
master2 <- data.frame(x = LETTERS[1:10], y2 = sample(1:100,10))
master10 <- data.frame(x = LETTERS[1:10], y3 = sample(1:100,10))
DF_obj <- lapply(ls(pattern = ".*master"), get)
library(dplyr);library(purrr)
gendf <- DF_obj %>% reduce(left_join, by = "x")

How to overcome this problem? Thanks.

Upvotes: 0

Views: 143

Answers (1)

AnilGoyal
AnilGoyal

Reputation: 26218

You have to replace x with your actual id column in the data to eliminate warning/error messages.

You may do either of two

base R

``` r
master1 <- data.frame(x = LETTERS[1:10], y1 = sample(1:100,10))
master2 <- data.frame(x = LETTERS[1:10], y2 = sample(1:100,10))
master10 <- data.frame(x = LETTERS[1:10], y3 = sample(1:100,10))
DF_obj <- lapply(ls(pattern = ".*master"), get)

gendf <- Reduce(function(.x, .y) merge(.x, .y, by = 'x'), x = DF_obj[-1], init = DF_obj[1]) 
  
gendf[, order(names(gendf))]
#>    x y1 y2  y3
#> 1  A 37 86  61
#> 2  B  3 23  89
#> 3  C 69 46  95
#> 4  D 16  9  54
#> 5  E 62 85  52
#> 6  F 19  5  35
#> 7  G 55 28  90
#> 8  H 40 52   5
#> 9  I  7 48 100
#> 10 J 48 16   9

tidyverse

master1 <- data.frame(x = LETTERS[1:10], y1 = sample(1:100,10))
master2 <- data.frame(x = LETTERS[1:10], y2 = sample(1:100,10))
master10 <- data.frame(x = LETTERS[1:10], y3 = sample(1:100,10))
DF_obj <- lapply(ls(pattern = ".*master"), get)

library(tidyverse)
purrr::reduce(DF_obj[-1], .init = DF_obj[1], ~ .x %>% as.data.frame() %>% left_join(.y, by = 'x'))
#>    x y1  y3 y2
#> 1  A 77  87 93
#> 2  B 10  18 74
#> 3  C  2  89 64
#> 4  D 89  98  5
#> 5  E 13  99 21
#> 6  F 74  25  4
#> 7  G 87   4 22
#> 8  H 62  27 17
#> 9  I 14  10 99
#> 10 J 21 100 78

Created on 2021-05-21 by the reprex package (v2.0.0)

Since the random seed has not been fixed, the results are different in two reprexes.

Upvotes: 1

Related Questions