Join list of data.frames using map() call

0 votes

I have a list of data.frames  and would like to merge them as part of a pipeline. Below is my attempt to do so.

library(tidyverse)

## Function to make a data.frame with an ID column and a random variable column with mean = df_mean
make.df <- function(df_mean){
  data.frame(id = 1:50,
             x = rnorm(n = 50, mean = df_mean))
}

## What I'd love:
my.dfs <- map(c(5, 10, 15), make.df) #%>%
  # <<some magical function that will full_join() on a list of data frames?>>

## Gives me the result I want, but inelegant
my.dfs.joined <- full_join(my.dfs[[1]], my.dfs[[2]], by = 'id') %>%
  full_join(my.dfs[[3]], by = 'id')

## Kind of what I want, but I want to merge, not bind
my.dfs.bound <- map(c(5, 10, 15), make.df) %>%
  bind_cols()

Apr 5, 2018 in Data Analytics by CodingByHeart77
• 3,680 points
19 views

1 answer to this question.

0 votes

You can use Reduce

set.seed(24)
r1 <- map(c(5, 10, 15), make.df)  %>% 
           Reduce(function(...) full_join(..., by = "id"), .)

Or you can try this with reduce

library(purrr)
set.seed(24)
r2 <- map(c(5, 10, 15), make.df)  %>%
             reduce(full_join, by = "id")

identical(r1, r2)
#[1] TRUE
answered Apr 5, 2018 by darklord
• 6,140 points

Related Questions In Data Analytics

0 votes
1 answer

How to join a list of data frames using map()

You can use reduce set.seed(24) r1 <- map(c(5, 10, ...READ MORE

answered Apr 11, 2018 in Data Analytics by kappa3010
• 2,020 points
44 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,700 points
68 views
0 votes
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,700 points
78 views
0 votes
1 answer

How to merge data frames using joins?

You can use the merge function with ...READ MORE

answered Apr 12, 2018 in Data Analytics by kappa3010
• 2,020 points
64 views
0 votes
4 answers

How to remove NA values with dplyr::filter()

Can we create a alist as below ...READ MORE

answered Aug 5 in Data Analytics by anonymous
6,231 views
0 votes
1 answer
0 votes
1 answer

How can I use parallel so that it preserves the list of data frames

You can use pmap as follows: nc <- ...READ MORE

answered Apr 4, 2018 in Data Analytics by kappa3010
• 2,020 points
34 views
0 votes
1 answer
0 votes
1 answer

How to use dplyr functions such as filter() inside nested data frames with map()

You can use map() call as follows:  map(full, ...READ MORE

answered Apr 6, 2018 in Data Analytics by darklord
• 6,140 points
230 views
0 votes
1 answer

How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,140 points
138 views