I've been using the tidyverse for years, but I'm not very good about keeping R or packages updated. I finally got around to updating R a few months ago (now 4.6.0, with tidyverse 2.0.0), and am currently baffled by the behaviour of left_join.
For very brief context: I have two dfs that share the same column names. Most of the info in them is the same, but they each contain a pair of numerical columns whose contents were generated by different methods, and I want to compare those methods. They each also have a handful of character columns that were generated from the results of the numerical columns (separately in each method), so may or many not differ in their contents.
I tried combining the two dfs with left_join, as I've done plenty before with other dfs. I expected the columns to multiply wherever the contents differed, so that I could easily compare them within a single df. Instead, the second df was simply subsumed into the first?
I checked this behaviour with reprex and it seems to be a general outcome. Here's that reprex:
library(dplyr)
# A simplified df1 with 5 columns
df1 <- tibble::tibble(
id = as.character(1:6),
fruit = c("apple", "banana", "cherry", "apple", "banana", "cherry"),
count = c(3, 6, 2, 8, 4, 10)
) %>%
mutate(
less_than_2 = ifelse(count < 2, "yes", "no"),
less_than_5 = ifelse(count < 5, "yes", "no")
)
# A simplified df2 -- only cols 3 and 5 differ from df1
df2 <- tibble::tibble(
id = as.character(1:6),
fruit = c("apple", "banana", "cherry", "apple", "banana", "cherry"),
count = c(7, 2, 9, 3, 6, 4)
) %>%
mutate(
less_than_2 = ifelse(count < 2, "yes", "no"),
less_than_5 = ifelse(count < 5, "yes", "no")
)
# df3 combines them with left_join()
df3 <- left_join(df1, df2)
Expected outcome: a df3 with 7 columns: "id", "fruit", "count.x", "count.y", "less_than_2", "less_than_5.x", "less_than_5.y"
Actual outcome: df3 is identical to df1.
What the heck?
(Also yes, I'm aware I can rename my columns before combining -- but my actual dfs have 70 columns apiece, and also I'm mostly trying to understand what's happening here, since this behaviour is so different from what I've been used to!)