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 6, 2018 in Data Analytics by CodingByHeart77
• 3,750 points
1,072 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 6, 2018 by Sahiti
• 6,370 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,090 points
1,609 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
1,397 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,770 points
1,174 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
1,710 views
+1 vote
5 answers

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

Try this: df %>% filter(!is.na(col1)) READ MORE

answered Mar 26, 2019 in Data Analytics by anonymous
332,261 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
1,106 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 Sahiti
• 6,370 points
4,732 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 14, 2018 in Data Analytics by Sahiti
• 6,370 points
7,104 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP