Eldar
Eldar

Reputation: 5237

Merge two lists with dataframes into one list with merged dataframes

I have two lists of data frames: listA and listB. How to get a list of merged dataframes (listC)?

dfA1 <- data.frame(x1 = c("a", "b"), y1 = c(1, 2), row.names = c("1", "2"))
dfA2 <- data.frame(x1 = c("c", "d"), y1 = c(3, 4), row.names = c("1", "3"))
dfB1 <- data.frame(x2 = c("c", "d"), y2 = c(3, 4), row.names = c("1", "2"))
dfB2 <- data.frame(x2 = c("e", "f"), y2 = c(5, 6), row.names = c("2", "3"))

listA <- list(dfA1, dfA2) # first input list
listB <- list(dfB1, dfB2) # second input list

m1 <- merge(dfA1, dfB1, by = 0, all = T)
m2 <- merge(dfA2, dfB2, by = 0, all = T)
listC <- list(m1, m2) # desired output list

Upvotes: 2

Views: 1082

Answers (1)

Eldar
Eldar

Reputation: 5237

Found following solution:

listC <- mapply(function(x, y) merge(x, y, by = 0, all = T), x = listA, y = listB, SIMPLIFY = F)

Upvotes: 2

Related Questions