将带有数据帧的两个列表合并到一个包含合并数据帧的列表中

时间:2015-02-17 18:54:25

标签: r merge dataframe

我有两个数据框列表:listA和listB。如何获取合并数据帧列表(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

1 个答案:

答案 0 :(得分:2)

找到以下解决方案:

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