Find and remove a repeated row 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. No person has yet followed the steps, so a screen may differ. Tell us what you find.
A row written twice adds its value to every total twice. Removing it is easy. The part that needs thought is deciding that the two rows really are one reading.
You will: have a table with the repeated row removed, and a note of what you removed and why.
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¶
-
Show every row that has an exact copy, keeping both copies in view:
readings[duplicated(readings) | duplicated(readings, fromLast = TRUE), ]You should see: one pair.
duplicated()alone marks only the second of a pair;fromLast = TRUEmarks the first.gauge_id date value flag 63 G03 2026-07-05 22.5 <NA> 64 G03 2026-07-05 22.5 <NA> -
Decide what to keep. Read the pair: the gauge, the day, the value, and the flag are all the same, and the data dictionary says one row is one reading from one gauge for one day.
You should see: no difference between the two rows. They carry the same information, so one can go and nothing is lost.
-
Remove the copy, into a new table:
deduped <- unique(readings) nrow(readings) - nrow(deduped)You should see: the number of rows removed.
readingsitself is unchanged.[1] 1 -
Check for the harder case, two rows for the same gauge and day that disagree:
sum(duplicated(deduped[c("gauge_id", "date")]))You should see:
0. Anything above zero would be two different readings for one day.unique()leaves those alone, and so should you until you know which is right.[1] 0 -
See what the copy was doing to that gauge's total:
c(before = sum(readings$value[readings$gauge_id == "G03"]), after = sum(deduped$value[deduped$gauge_id == "G03"]))You should see: the total fall by the value of the repeated row.
before after 273.6 251.1 -
In a script or a notes file, record what you removed: the gauge, the day, the value, and that the two rows were identical.
You should see: your note where a reader can tell the row was removed on purpose.
Check it worked¶
Run step 1 on the new table, with deduped in place of readings. It prints
<0 rows>, and your note says which row went.
If it didn't¶
Step 1 prints <0 rows>. The table was already cleaned, or readings was
replaced by another table. Read the file again, as in
Load a file from your Data Store in RStudio, then repeat the step.
Next¶
- Count records per day in R, which finds the same row another way, and the days with no row at all.
- Bring two units onto one scale in R
- The same task in Python: Find and remove a repeated row.
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.