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
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