我尝试使用dplyr' s full_join
来合并两个data.frames,例如:
col1 = 'b'
col2 = 'd'
df1 = data.frame(a = 1:3, b = 1:3)
df2 = data.frame(a = 1:3, d = 1:3)
full_join(df1, df2, c('a' = 'a', col1 = col2))
但它返回
错误:
by
无法包含LHS中缺少的连接列col1
我正在寻找类似于
的输出merge(df1, df2, by.x = c('a', col1), by.y = c('a', col2))
a b
1 1 1
2 2 2
3 3 3
答案 0 :(得分:4)
您可以使用rename_
,即
library(dplyr)
full_join(df1, rename_(df2, .dots = setNames(col2, col1)))
给出,
#Joining, by = c("a", "b") a b 1 1 1 2 2 2 3 3 3
根据@akrun和@ mt1022评论发布替代方案,
#akrun
full_join(df1, rename_at(df2, .vars = col2, funs(paste0(col1))))
full_join(df1, rename(df2, !!(col1) := !!rlang::sym(col2)))
#mt1022
full_join(df1, rename_at(df2, col2, ~col1))
答案 1 :(得分:1)
像这样更改联接:
full_join(df1, df2, by=c('b'='d'))
a.x b a.y
1 1 1 1
2 2 2 2
3 3 3 3
答案 2 :(得分:1)
duplicate link中对@MrFlick的所有信用,稍微修改了OP
的示例:
full_join(df1, df2, by = c("a",setNames(col2, col1)))
答案 3 :(得分:0)
这会重现你的结果,这是你想要的吗?
full_join(df1, df2, by="a")%>%select(-d)