gpt4 book ai didi

r - dplyr:如何按名称选择连接列?

转载 作者:行者123 更新时间:2023-12-02 10:57:36 27 4
gpt4 key购买 nike

我想使用 dplyr 的 left_join 将值("new")从一个 DF 传输到另一个 DF。

如果我不知道键的名称,但只知道它是数据集中的第一个变量,我该怎么做?

require("dplyr")

testData1 <- data.frame(idvar=c(1,2,3),
b=c("a","b","c"),
c=c("i","ii","iii"))

testData2 <- data.frame(identification=c(1,2),
b=c("a","b"),
c=c("i","NA"),
new=c("var1","var2"))

# now do a left join to obtain values of the new variable in the old dataset


(testResult1 <- left_join(testData1,testData2))
# var2 is not in the results because of the "NA" in testData2!


(testResult2 <- left_join(testData1,testData2,
by=c("idvar"="identification")))
# works as expected! ... but we do not know the name of the idvar!


(testResult3 <- left_join(testData1,testData2,
by=c(names(testData1)[1]=names(testData2)[1])))
# Error: unexpected '=' in:
# "testResult3 <- left_join(testData1,testData2,
# by=c(names(testData1)[1]="

最佳答案

另一种方法是使两个键列具有相同的名称:

left_join(
testData1,
rename_at(testData2, 1, ~ names(testData1)[1]),
by = names(testData1)[1]
)

# idvar b.x c.x b.y c.y new
# 1 1 a i a i var1
# 2 2 b ii b NA var2
# 3 3 c iii <NA> <NA> <NA>

# > (testResult2 <- left_join(testData1,testData2, by=c("idvar"="identification")))
# idvar b.x c.x b.y c.y new
# 1 1 a i a i var1
# 2 2 b ii b NA var2
# 3 3 c iii <NA> <NA> <NA>

关于r - dplyr:如何按名称选择连接列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45507044/

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