我想要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
这对您有用吗?
jnd.tbl <- df1 %>% left_join(df2, by='b') %>% left_join(df3, by='d')
我知道已经太晚了......今天我被介绍到了未解答的问题部分.抱歉打扰.
运用 left_join()
dfs <- list( df1 = data.frame(b = c("a", "b", "c"), a = 1:3), df2 = data.frame(d = c("a", "c", "d"), c = 4:6), df3 = data.frame(b = c("b", "c", "e"), d = 7:9) ) func <- function(...){ df1 = list(...)[[1]] df2 = list(...)[[2]] col1 = colnames(df1)[1] col2 = colnames(df2)[1] xxx = left_join(..., by = setNames(col2,col1)) return(xxx) } Reduce( func, dfs) # b a c d #1 a 1 4 NA #2 b 2 NA 7 #3 c 3 5 8
使用merge()
:
func <- function(...){ df1 = list(...)[[1]] df2 = list(...)[[2]] col1 = colnames(df1)[1] col2 = colnames(df2)[1] xxx=merge(..., by.x = col1, by.y = col2, , all.x = T) return(xxx) } Reduce( func, dfs) # b a c d #1 a 1 4 NA #2 b 2 NA 7 #3 c 3 5 8