我想使用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 –