Remove same columns from left_join

前端 未结 4 1958
野趣味
野趣味 2021-01-19 01:23

I\'d like to merge two data frames by id, but they both have 2 of the same columns; therefore, when I merge i get new .x and .y column

4条回答
  •  无人共我
    2021-01-19 02:07

    You only need:

    df <- left_join(df1, df2)
    

    by = NULL, the default, join will do a natural join, using all variables with common names across the two tables. A message lists the variables so that you can check they're right

    Output:

    Joining by: c("id", "element", "day")
      id     value1 element day     value2
    1  1 -0.6264538   TEST1  15 -0.8204684
    2  2  0.1836433   TEST1  15  0.4874291
    3  3 -0.8356286   TEST1  15  0.7383247
    4  4  1.5952808   TEST1  15  0.5757814
    5  5  0.3295078   TEST1  15 -0.3053884
    

    It's worth pointing out the comment by thelatemail: "Joining on id is not the same as joining on id/element/day". However, in this specific example, because element and day are the same for all records in both tables we get the same result.

    Original result

    Data

    set.seed(1)
    df1 <- data.frame(id = seq(1,5), value1 = rnorm(5), element = "TEST1", day = 15) 
    df2 <- data.frame(id = seq(1,5), value2 = rnorm(5), element = "TEST1", day = 15) 
    df <- left_join(df1, df2, by = "id")
    

    Output:

      id     value1 element.x day.x     value2 element.y day.y
    1  1 -0.6264538     TEST1    15 -0.8204684     TEST1    15
    2  2  0.1836433     TEST1    15  0.4874291     TEST1    15
    3  3 -0.8356286     TEST1    15  0.7383247     TEST1    15
    4  4  1.5952808     TEST1    15  0.5757814     TEST1    15
    5  5  0.3295078     TEST1    15 -0.3053884     TEST1    15
    

提交回复
热议问题