Work with a long table 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.
Many archives store data long: one row per time and per kind of measurement, with
the number in a single value column. It is a tidy way to store data and an easy way
to average rainfall with temperature by accident.
You will: have the hourly observations summarised by measurement and reshaped to one column for each.
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¶
-
In the Console, fetch the hourly observations into your practice folder:
download.file("https://idss-mesa.github.io/trainings/assets/practice/observations.csv", "observations.csv")You should see: a line that reads
downloaded 13 KB. -
Read the file and check its size:
obs <- read.csv("observations.csv") dim(obs)You should see:
[1] 336 4 -
Look at the first rows:
head(obs, 5)You should see: each timestamp twice, once for each kind of measurement.
gauge_id timestamp_utc measurement value 1 G03 2026-07-10T06:00:00Z rain_mm 0.0 2 G03 2026-07-10T06:00:00Z air_temp_c 13.8 3 G03 2026-07-10T07:00:00Z rain_mm 0.0 4 G03 2026-07-10T07:00:00Z air_temp_c 12.3 5 G03 2026-07-10T08:00:00Z rain_mm 0.0 -
Count the rows for each kind:
table(obs$measurement)You should see:
air_temp_c rain_mm 168 168 -
Summarise each kind on its own:
aggregate(value ~ measurement, data = obs, FUN = function(v) round(c(min = min(v), max = max(v), mean = mean(v)), 2))You should see: two rows with nothing in common.
mean(obs$value)would have returned 9.27, a number that is neither millimetres nor degrees.measurement value.min value.max value.mean 1 air_temp_c 10.20 25.40 18.03 2 rain_mm 0.00 15.80 0.51 -
Reshape to one column per measurement, and check the size:
wide <- reshape(obs[c("timestamp_utc", "measurement", "value")], idvar = "timestamp_utc", timevar = "measurement", direction = "wide") head(wide, 3) dim(wide)You should see: one row for each hour, with the timestamp and a column for each kind of measurement.
timestamp_utc value.rain_mm value.air_temp_c 1 2026-07-10T06:00:00Z 0 13.8 3 2026-07-10T07:00:00Z 0 12.3 5 2026-07-10T08:00:00Z 0 10.7 [1] 168 3
Check it worked¶
The counts reconcile: 168 hours × 2 measurements is 336, the number of rows you
started with, and wide has no empty cells (sum(is.na(wide)) is 0).
If it didn't¶
Warning message: ... multiple rows match for measurement=...: first taken. Some
timestamp has two rows for the same measurement. R kept the first and dropped the
other, and only warned. Find them with
obs[duplicated(obs[c("timestamp_utc", "measurement")]) | duplicated(obs[c("timestamp_utc", "measurement")], fromLast = TRUE), ]
and settle which is right before you reshape.
Next¶
- Turn UTC timestamps into local days in R, which uses the same file.
- Query a Parquet file with DuckDB from R
- The same task in Python: Work with a long table, one row per kind of measurement.
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.