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,100 points
20 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,010 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 darklord
• 6,140 points
8 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
32 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
61 views
0 votes
1 answer

How to change the value of a variable using R programming in a data frame?

Try this: df$symbol <- as.character(df$symbol) df$symbol[df$symb ...READ MORE

answered Jan 11 in Data Analytics by Tyrion anex
• 8,280 points
46 views
0 votes
1 answer
0 votes
1 answer
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
31 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
• 35,720 points
21 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,010 points
18 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.