Count records per day 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.
The data dictionary says one row is one reading for one gauge on one day. This
tutorial tests that claim. A day counted twice inflates a total, and a missing day
shrinks it, and neither shows up as an NA.
You will: know, for each gauge, whether July has exactly one row per day.
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¶
-
Work out what a complete table would hold: 5 gauges × 31 days in July.
You should see: 155. The table has 152 rows, so something is uneven.
-
Count the rows for each gauge:
table(readings$gauge_id)You should see:
G01 G02 G03 G04 G05 27 31 32 31 31 -
Count the distinct days for each gauge:
tapply(readings$date, readings$gauge_id, function(d) length(unique(d)))You should see: G03 with 31 days, not 32, and G01 still with 27.
G01 G02 G03 G04 G05 27 31 31 31 31 -
Find any gauge and day with more than one row:
per_day <- aggregate(value ~ gauge_id + date, data = readings, FUN = length) per_day[per_day$value != 1, ]You should see: one gauge and day, where the
valuecolumn now holds the number of rows.gauge_id date value 23 G03 2026-07-05 2 -
List the days on which G01 has no row:
july <- as.character(seq(as.Date("2026-07-01"), as.Date("2026-07-31"), by = "day")) setdiff(july, readings$date[readings$gauge_id == "G01"])You should see: four consecutive days.
[1] "2026-07-22" "2026-07-23" "2026-07-24" "2026-07-25" -
In a script or a notes file, note both findings and what each does to a monthly total.
You should see: your note where a reader of your result can find it.
Check it worked¶
The numbers reconcile: 155 expected rows, plus 1 repeated row, minus 4 missing days, is 152, the size of the table.
If it didn't¶
Step 4 prints <0 rows>. You ran it on a table from which repeated rows had
already been removed, for example with unique(). Read the file again, then repeat
the step.
Step 5 lists all 31 days. date is no longer text. setdiff() compares like with
like, so compare text with text: use as.character(readings$date).
Next¶
- Query a Parquet file with DuckDB from R
- Reconcile row counts before and after a join in R
- The same task in Python: Count records per day to find incomplete days.
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.