我想使用dplyr的left_join将一个数据框中的值(“new”)转移到另一个数据框中。如果我不知道关键字的名称,但只知道它是数据集中的第一个变量,我该如何做到这一点?
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]="