Tally a quality flag in R¶
Draft: not yet validated
The code on this page was run inside MESA RStudio Geospatial on 2026-11-13 (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.
Many datasets mark the readings their instrument doubted. table() counts them, but by
default it leaves missing values out, which hides how many rows carry no flag at all.
You will: know how many readings are flagged, which ones, and at which gauges.
Time: 5 minutes. You need: the readings table from
Load a file from your Data Store in RStudio, in
MESA RStudio Geospatial.
Steps¶
-
Read what the flag means in the data dictionary.
You should see: blank, or
Sfor a reading the sensor marked suspect. -
Count the rows for each flag value, missing ones included:
table(readings$flag, useNA = "always")You should see:
S <NA> 2 150 -
Check that nothing is left over: 2 + 150 = 152, the number of rows.
You should see: the counts add up, and no flag value the dictionary does not list.
-
Look at the flagged rows:
readings[which(readings$flag == "S"), ]You should see:
gauge_id date value flag 77 G03 2026-07-18 0 S 130 G05 2026-07-09 180 S -
Count flags by gauge:
table(readings$gauge_id, readings$flag, useNA = "ifany")You should see:
S <NA> G01 0 27 G02 0 31 G03 1 31 G04 0 31 G05 1 30 -
In a script or a notes file, write down what you will do with flagged readings and why: keep them, leave them out, or report the result both ways.
You should see: your decision recorded where a reader of your result can find it.
Check it worked¶
You can say how many readings are flagged (2), where they are, and what your analysis does with them. One is far outside its gauge's range; the other is an ordinary dry day.
If it didn't¶
Step 4 prints 152 rows, 150 of them all NA. You wrote readings[readings$flag == "S", ]
without which(). Comparing a missing value with "S" gives NA, and R returns a row
of NA for each one. which() keeps only the rows where the comparison is true.
Next¶
- Query a Parquet file with DuckDB from R
- The same task in Python: Tally a quality flag before you trust a number.
Not yet validated by a person in MESA RStudio Geospatial. Drafted with AI assistance on 2026-11-13; 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.