如何使用dplyr中的*_join()函数连接两个表格,当连接变量名称不同时且存储在另一个变量中?
例如:
但是这会出现错误 -
例如:
df1 = data_frame(x1 = 1:10,y1 = 21:30)
df2 = data_frame(x2 = 6:15,y2 = 26:35)
df3 = data_frame(x1 = 6:15,y2 = 26:35)
var1 = "x1"
var2 = "x2"
df1 %>% left_join(df3,by=c(var1)) # #1 works
但是这会出现错误 -
df1 %>% left_join(df2,by=c(var1 = var2)) # #2 doesn't work
Error: cannot join on columns 'x2' x 'var1': index out of bounds
令人惊讶的是,这个方法有效 -
df1 %>% left_join(df2,by=c("x1" = var2)) # #3 works
names<-
的恶劣方式 -setNames()
更好。 - hadley