Reconcile row counts before and after a join in R¶
Draft: not yet validated
The code on this page was run inside MESA RStudio Geospatial on 2026-11-16 (R 4.6.2), by the AI assistant that drafted it, and produced the output shown, one step at a time. No person has yet followed the steps, so a screen may differ. Tell us what you find.
A join can silently add rows, when a key repeats in the table you look things up in, or lose them, when a key has no match. Counting rows before and after catches both.
You will: have each reading joined to its gauge's name and unit, with row counts that prove the join changed nothing else.
Time: 5 to 10 minutes. You need: the readings and gauges tables from
Load a file from your Data Store in RStudio, in
MESA RStudio Geospatial.
Steps¶
-
Record the size of the table you are starting from:
nrow(readings)You should see:
[1] 152 -
Confirm that the lookup table has one row for each key:
anyDuplicated(gauges$gauge_id)You should see:
0, meaning no gauge appears twice. Any other number is the position of the first repeat, and every reading of that gauge would be doubled by the join.[1] 0 -
Join:
joined <- merge(readings, gauges, by = "gauge_id", all.x = TRUE)You should see: no output and no error.
all.x = TRUEkeeps every reading, whether or not it finds a gauge. Without it,merge()drops the readings that have no match and says nothing. -
Compare the sizes:
c(nrow(readings), nrow(joined))You should see:
[1] 152 152 -
Count the readings that found no gauge:
sum(is.na(joined$name))You should see:
[1] 0 -
Look at the result:
head(joined, 3)You should see: the four original columns, then the gauge's columns.
gauge_id date value flag name unit elevation_m 1 G01 2026-07-01 0.0 <NA> Boathouse mm 512 2 G01 2026-07-02 0.0 <NA> Boathouse mm 512 3 G01 2026-07-03 24.9 <NA> Boathouse mm 512
Check it worked¶
Three numbers agree: 152 rows before, 152 rows after, and 0 readings without a match.
Each reading now carries its unit, which you need before values from different
gauges can be compared.
If it didn't¶
The second number in step 4 is larger than the first. The lookup table has a
repeated gauge_id, and merge() multiplied rows without a warning. Find the repeats
with gauges[gauges$gauge_id %in% gauges$gauge_id[duplicated(gauges$gauge_id)], ] and
settle which row is right before you join.
The rows of joined are in a different order from readings. merge() sorts its
result by the key. Count and compare; do not rely on row positions.
Next¶
- Leave an R script someone else can rerun
- A join carries problems along with it: rows repeated in
readingsare still repeated injoined. See Count records per day in R. - The same task in Python: Reconcile row counts before and after a join.
Not yet validated by a person in MESA RStudio Geospatial. Drafted with AI assistance on 2026-11-16; the code was run there that day (R 4.6.2).
Machine-readable versions of this page: Markdown twin · raw source on GitHub · llms.txt · llms-full.txt (whole site). See For AI agents.