How to specify names of columns for x and y when joining in dplyr?

前端 未结 2 1707
心在旅途
心在旅途 2020-12-22 20:40

I have two data frames that I want to join using dplyr. One is a data frame containing first names.

test_data <- data.frame(first_name = c(\"john\", \"bil         


        
相关标签:
2条回答
  • 2020-12-22 21:29

    This feature has been added in dplyr v0.3. You can now pass a named character vector to the by argument in left_join (and other joining functions) to specify which columns to join on in each data frame. With the example given in the original question, the code would be:

    left_join(test_data, kantrowitz, by = c("first_name" = "name"))
    
    0 讨论(0)
  • 2020-12-22 21:41

    This is more a workaround than a real solution. You can create a new object test_data with another column name:

    left_join("names<-"(test_data, "name"), kantrowitz, by = "name")
    
         name gender
    1    john      M
    2    bill either
    3 madison      M
    4    abby either
    5     zzz   <NA>
    
    0 讨论(0)
提交回复
热议问题