gpt4 book ai didi

r - full_join 通过变量作为列名

转载 作者:行者123 更新时间:2023-12-04 10:51:50 24 4
gpt4 key购买 nike

我正在尝试使用 dplyr 的 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))

但它返回

Error: by can't contain join column col1 which is missing from LHS



我正在寻找类似于的输出
merge(df1, df2, by.x = c('a', col1), by.y = c('a', col2))
a b
1 1 1
2 2 2
3 3 3

最佳答案

您可以使用 rename_ , IE。,

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))

关于r - full_join 通过变量作为列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49111080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com