如何使用dplyr连接多个数据框?

时间:2015-12-17 21:04:53

标签: r dplyr reduce

我想left_join多个数据框:

dfs <- list(
  df1 = data.frame(a = 1:3, b = c("a", "b", "c")),
  df2 = data.frame(c = 4:6, b = c("a", "c", "d")),
  df3 = data.frame(d = 7:9, b = c("b", "c", "e"))
)
Reduce(left_join, dfs)
#   a b  c  d
# 1 1 a  4 NA
# 2 2 b NA  7
# 3 3 c  5  8

这是有效的,因为它们都有相同的b列,但Reduce不允许我指定我可以传递给left_join的其他参数。是否有类似这样的工作?

dfs <- list(
  df1 = data.frame(a = 1:3, b = c("a", "b", "c")),
  df2 = data.frame(c = 4:6, d = c("a", "c", "d")),
  df3 = data.frame(d = 7:9, b = c("b", "c", "e"))
)

更新

此类作品:Reduce(function(...) left_join(..., by = c("b" = "d")), dfs)但当by不止一个元素时会出现此错误:Error: cannot join on columns 'b' x 'd': index out of bounds

1 个答案:

答案 0 :(得分:5)

这对你有用吗?

jnd.tbl <- df1 %>%
    left_join(df2, by='b') %>%
    left_join(df3, by='d')