Ian_De_Oliveira
Ian_De_Oliveira

Reputation: 291

Merge multiple data frames in a specific order R

I have 2 lists containing 5 data frames each, and they are in a specific order ;

list_1<-list("A","B","C","D")

list_2 <-list(("Aa","Bb","Cc","Dd")

I need to left_join A to Aa, B to Bb and so on, therefore reduce is not a option as I'm aware. Any help is greatly appreciated.

I though about using lapply but I failed.

Upvotes: 1

Views: 113

Answers (1)

akrun
akrun

Reputation: 887168

As we are joining the dataset in the corresponding order, it would be good to use Map. Assuming that we are storing the object names as strings in another object ('list_1' or 'list_2'), it is better to have it as a vector instead of list and then do mget to get the values of the objects

Map(function(x, y) merge(x, y, all.x = TRUE), mget(list_1), mget(list_2))

Or using tidyverse

library(tidyverse)
map2(mget(list_1), mget(list_2), left_join)

data

list_1<- c("A","B","C","D")
list_2 <- c("Aa","Bb","Cc","Dd")

Upvotes: 2

Related Questions