Josh
Josh

Reputation: 21

Using R and purrr to join multiple dataframes using a list of lists with pmap

I'm trying to join together dataframes that are embedded in a list using pmap.

library(purrr)
library(plyr)
# Create a list of 5 data frames
create_df <- function(){
      map(1:5, ~ data.frame(country = c("USA", "CHINA", "JAPAN", "FRANCE"),
                                         col = sample.int(100, 4))
      )
    }
# Create a list of lists
list_of_list_of_dataframes <- map(1:3, ~ create_df())
# join the first element of the 3 lists together. 
list_of_dataframes <- pmap(list_of_list_of_dataframes, join_all, by = 'country', type = 'left')

The problem that I have is that this join_all function from plyr doesn't seem to work. I get the error: Error in .f(.l[[c(1L, i)]], .l[[c(2L, i)]], .l[[c(3L, i)]], ...) : unused argument (.l[[c(3, i)]])

I am able to do list_of_dataframes <- pmap(list_of_list_of_dataframes, cbind) to bind the data frames together but it doesn't delete the index columns that I'm matching too and assumes that the order of the left columns is the same.

Upvotes: 2

Views: 3297

Answers (2)

phiver
phiver

Reputation: 23608

You can use dplyr's left_join + purrr's reduce and map2. But it is less readable than @Tung's answer.

reduce(list_of_list_of_dataframes, map2, left_join, by = "country")
[[1]]
  country col.x col.y col
1     USA    82    31  59
2   CHINA     7    65  29
3   JAPAN    62    58  52
4  FRANCE    70    88  35

[[2]]
  country col.x col.y col
1     USA    17    77  23
2   CHINA     5    86  92
3   JAPAN    70    55  73
4  FRANCE    68    42  13

[[3]]
  country col.x col.y col
1     USA    51    10  20
2   CHINA    60    82  65
3   JAPAN    65    90  56
4  FRANCE    64    30  10

[[4]]
  country col.x col.y col
1     USA     9    53  36
2   CHINA    60     5  89
3   JAPAN    51    88  69
4  FRANCE     6    14  11

[[5]]
  country col.x col.y col
1     USA    63    30  94
2   CHINA    92    16  21
3   JAPAN    11    52  27
4  FRANCE    58   100  66

Upvotes: 3

Tung
Tung

Reputation: 28451

Easiest way would be to use bind_cols and then select only desired columns

library(tidyverse)

set.seed(123456)

# Create a list of 5 data frames
create_df <- function(){
  map(1:5, ~ data.frame(country = c("USA", "CHINA", "JAPAN", "FRANCE"),
                        col = sample.int(100, 4))
  )
}

# Create a list of lists
list_of_list_of_dataframes <- map(1:3, ~ create_df())
str(list_of_list_of_dataframes, max.level = 1)

#> List of 3
#>  $ :List of 5
#>  $ :List of 5
#>  $ :List of 5

pmap(list_of_list_of_dataframes, bind_cols) %>% 
  map(~ select(.x, country, matches("col")))

#> [[1]]
#>   country col col1 col2
#> 1     USA  80   16   23
#> 2   CHINA  75    8   74
#> 3   JAPAN  39   13   88
#> 4  FRANCE  34   17   29
#> 
#> [[2]]
#>   country col col1 col2
#> 1     USA  37   48   52
#> 2   CHINA  20   70   44
#> 3   JAPAN  53   87   57
#> 4  FRANCE  10   86   72
#> 
#> [[3]]
#>   country col col1 col2
#> 1     USA  99   86   68
#> 2   CHINA  17   17   36
#> 3   JAPAN  79   52   33
#> 4  FRANCE  58   85   73
#> 
#> [[4]]
#>   country col col1 col2
#> 1     USA  91   24   51
#> 2   CHINA  88   12   86
#> 3   JAPAN  98   81   75
#> 4  FRANCE  87    8   94
#> 
#> [[5]]
#>   country col col1 col2
#> 1     USA  88   96   84
#> 2   CHINA  20   87   83
#> 3   JAPAN  33   70   27
#> 4  FRANCE  76   59   18

Created on 2018-07-05 by the reprex package (v0.2.0.9000).

Upvotes: 1

Related Questions