How to join a list of data frames using map

0 votes

Is there a tidyverse way to join a list of data frames?

I have a list of data.frames as a result of a call to map().

I have used reduce() to do something like this before, but I want to merge them as apart of pipeline. Consider the below example for reference:

library(tidyverse)

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

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

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

## This gives what I want but I don't want to bind. Instead I want to merge
my.dfs.bound <- map(c(5, 10, 15), make.df) %>%
  bind_cols()
Apr 11, 2018 in Data Analytics by DataKing99
• 8,240 points
327 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 also 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 11, 2018 by kappa3010
• 2,090 points

Related Questions In Data Analytics

0 votes
1 answer

Join list of data.frames using map() call

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

answered Apr 5, 2018 in Data Analytics by Sahiti
• 6,380 points
398 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,720 points
426 views
0 votes
1 answer

How to combine a list of data frames into one data frame?

Use bind_rows() from the dplyr package: bind_rows(list_of_dataframes, .id ...READ MORE

answered Dec 17, 2020 in Data Analytics by Gitika
• 65,870 points
189 views
+1 vote
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 Sahiti
• 6,380 points
2,415 views
0 votes
1 answer
0 votes
1 answer

How to apply list to a function which give data frame as output

If you use  tidyverse, you can use ...READ MORE

answered Apr 11, 2018 in Data Analytics by Sahiti
• 6,380 points
136 views
+1 vote
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,720 points
581 views
0 votes
2 answers

In data frame how to spilt strings into values?

You can do this using dplyr and ...READ MORE

answered Dec 4, 2018 in Data Analytics by Kalgi
• 52,310 points
237 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,090 points
168 views