Check a result a second way 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.
If two different routes give the same answer, a mistake would have to be made twice, in the same way, to go unnoticed. If they disagree, you have found something before your reader does. This works for a result of your own, a colleague's, or an AI assistant's.
You will: have counted the readings not flagged as suspect in two ways, explained why the counts differ, and reached a total that two routes agree on.
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, count the readings whose flag is not
S:nrow(subset(readings, flag != "S"))You should see:
[1] 0 -
Count them again, with square brackets:
odd <- readings[readings$flag != "S", ] nrow(odd)You should see: a different answer. At least one route is not doing what you meant.
[1] 150 -
Look at the rows the second route kept:
head(odd, 3)You should see: rows with nothing in them.
gauge_id date value flag NA <NA> <NA> NA <NA> NA.1 <NA> <NA> NA <NA> NA.2 <NA> <NA> NA <NA>Most flags are missing, and comparing a missing value with
"S"givesNA, neither true nor false.subset()drops those rows. Square brackets keep one empty row for each. Neither route kept the 150 unflagged readings. -
Say what you mean: keep a row if its flag is missing or is not
S.kept <- readings[is.na(readings$flag) | readings$flag != "S", ] nrow(kept)You should see:
[1] 150 -
Check the total rainfall in
keptby a route that does not usekept: the whole column, less the readings flaggedS.sum(kept$value) sum(readings$value) - sum(readings$value[which(readings$flag == "S")])You should see: the same number twice.
[1] 895.43 [1] 895.43
Check it worked¶
Two routes give 895.43, and you can say why step 1 counted no rows and why the 150 in step 2 was not the 150 you wanted.
If it didn't¶
Step 5 prints NA. kept was made with the square brackets of step 2, so it
holds empty rows. Make it again with the line in step 4.
The two routes agree but you doubt both. Agreement shows the two were computed the same way, not that the data is sound. This total still mixes units.
Next¶
- Check that the parts add up to the whole in R
- Tally a quality flag in R
- Reference: subset 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.