Check that the parts add up to the whole in R¶
Draft: not yet validated
The code on this page was run inside MESA RStudio Geospatial on 2026-11-17 (R 4.6.2), by the AI assistant that drafted it, and produced the output shown. No person has yet followed the steps, so a screen may differ. Tell us what you find.
Splitting a table into groups should lose nothing: the groups, added up, should give the table back. When they do not, rows have gone missing without an error message.
You will: have caught 150 rows that a tally dropped without warning, and confirmed a split that loses none.
Time: 10 minutes. You need: the readings and gauges tables from
Load a file from your Data Store in RStudio, in
MESA RStudio Geospatial.
Steps¶
-
In the Console, state the whole before you split it:
nrow(readings)You should see:
[1] 152 -
Count the rows for each flag:
parts <- table(readings$flag) partsYou should see: one group. Nothing warns you that anything is missing.
S 2 -
Add the parts up and put the total beside the whole:
c(sum(parts), nrow(readings))You should see: the parts 150 rows short.
[1] 2 152 -
The missing rows are the ones with no flag:
table()leaves out missing values unless told otherwise. Tell it otherwise:parts <- table(readings$flag, useNA = "ifany") partsYou should see:
S <NA> 2 150 -
Check again:
sum(parts) == nrow(readings)You should see:
[1] TRUE -
The same check works for a sum. Split the rainfall by gauge, and compare the groups' total with the column's:
by_gauge <- tapply(readings$value, readings$gauge_id, sum) c(sum(by_gauge), sum(readings$value))You should see: the same number twice. No row is missing its
gauge_id, so this split loses nothing.[1] 1075.43 1075.43
Check it worked¶
You have made two comparisons where the parts equal the whole: 152 rows by flag, and 1075.43 by gauge. You can say which rows the first tally dropped and why.
If it didn't¶
Step 3 shows [1] 152 152. parts was already made with useNA = "ifany". That
is the corrected version; run step 2 again to see the short one.
Step 6 shows NA. A value is missing somewhere in the column. Run
sum(is.na(readings$value)); in the practice data it is 0.
Next¶
- Tally a quality flag in R, which counts the missing flags directly.
- Check a result a second way in R
- Reference: table in the R documentation.
Not yet validated by a person in MESA RStudio Geospatial. Drafted with AI assistance on 2026-11-17; 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.