My join made extra rows. Why?
We join two tables. Before, we had 3 rows. After, we have 5. Nothing looks broken, but our totals are now wrong. This is one of the most common surprises in R. Here is why it happens and how to stop it for good.
What we need
- R 4.1 or newer
- dplyr 1.1 or newer
- Two small tables (we make them below)
Make two small tables
One table lists visits by ZIP code. The other gives each ZIP code a name. Notice that 10023 has two names. That is the trouble maker.
library(dplyr)
visits <- tibble(
zip = c("10023", "10023", "11211"),
visit = c("Mon", "Tue", "Wed")
)
zip_names <- tibble(
zip = c("10023", "10023", "11211"),
name = c("Lincoln Square", "Upper West Side", "Williamsburg")
)
nrow(visits)Loading the checked output…
Join them and count the rows
We want each visit with its neighborhood name, so we expect 3 rows back. Look at what we get, and at the warning dplyr gives us.
joined <- left_join(visits, zip_names, by = "zip")
nrow(joined)Loading the checked output…
Find the repeated key
Count each key in the lookup table. Any key with more than 1 will copy rows when we join.
zip_names |>
count(zip) |>
filter(n > 1)Loading the checked output…
Fix it: one name per ZIP code
Decide which name we want. The best fix is in the lookup table itself, so every later join is safe too.
zip_names_fixed <- zip_names |>
filter(name != "Upper West Side")
joined <- left_join(visits, zip_names_fixed, by = "zip")
nrow(joined)Loading the checked output…
Taste test: let R guard the join
Tell dplyr what we expect: many visits may share one name, but each visit must match only one name. If the lookup table ever gets a repeat again, R stops with a clear message instead of quietly adding rows.
joined <- left_join(
visits, zip_names_fixed, by = "zip",
relationship = "many-to-one"
)
stopifnot(nrow(joined) == nrow(visits))
nrow(joined)Loading the checked output…
See the guard catch the old table
Here is the same guard on the table with the repeated ZIP code. This time R stops and tells us which row is the problem.
left_join(
visits, zip_names, by = "zip",
relationship = "many-to-one"
)Loading the checked output…
Kitchen tip: the habit that prevents this
Before every join, ask: "Is the key unique in the table I am looking things up in?" Then let R check it for us with relationship = "many-to-one", and count the rows before and after.
Three common mix-ups
- Spaces or capitals that make two keys look different ("10023" and "10023 "), so rows go missing instead.
- A key stored as a number in one table and as text in the other, so ZIP code "07030" loses its zero.
- Turning the warning off with relationship = "many-to-many" without checking why it appeared.
Quick check
A table of 10 trips joins to a table where one station appears 3 times. 2 of the trips use that station. How many rows come out?
Practice: try it here
A shop has 4 orders. The store list has store S2 twice, so the join below makes 6 rows instead of 4. Change the code so joined has exactly one row per order.
Change the code in the box, then press Run to see what it does, and Check my answer when it looks right. It all runs in this browser, so nothing is sent anywhere.
The two S2 rows in stores are exactly the same. distinct() keeps one of each.
library(dplyr)
orders <- tibble(
order = 1:4,
store = c("S1", "S2", "S2", "S3")
)
stores <- tibble(
store = c("S1", "S2", "S2", "S3"),
town = c("Queens", "Bronx", "Bronx", "Brooklyn")
)
stores <- distinct(stores)
joined <- left_join(orders, stores, by = "store",
relationship = "many-to-one")
nrow(joined)- count(key) on the lookup table
- fix repeats at the source
- relationship = "many-to-one"
- rows before = rows after
Keep the recipe card
A one-picture summary to pin next to a screen or send to a friend who is stuck on the same thing.
Trying this on other data? Check the row counts and results before and after each step. Package versions change, so if something here works differently, please tell us. Every output on this page comes from running the code above in R; please cross-check before relying on it for our work.