我想使用dplyr的left_join将值(“新”)从一个DF转移到另一个DF。dplyr:如何按名称选择连接列?
我该怎么做,如果我不知道密钥的名称,但只知道它是数据集中的第一个变量?
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]="
这是一个相关的Q&A:https://stackoverflow.com/questions/28125816/r-standard-evalation-for- join-dplyr –