Unit 03.04: Four looks, four different questions
Four short commands, each answering a different question. Run all four before you transform anything.
Four looks, four questions
head() asks whether the first rows are what you expected — the fastest way to catch a file read with the wrong delimiter or an off-by-one header.
glimpse() asks what the columns are, what type each is, and what the values look like. It prints one line per column, so a wide table stays readable.
summary() asks about range and missingness for a numeric column: the minimum, the quartiles, the maximum and the NA count. Impossible values show up here immediately.
count() asks how many rows fall in each category, which is how you find 'North', 'north' and 'North ' living in the same column as three separate wards.
This block runs all four on a small table with one missing value.
suppressPackageStartupMessages({library(dplyr); library(tibble)})
ward_visits <- tibble(
ward = c("North", "South", "East", "North", "South", "East", "North"),
visits = c(412, 388, 502, 455, NA, 498, 431),
staffed = c(TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE)
)
cat("--- head(): are the first rows what I expect?\n")
print(head(ward_visits, 3))
cat("\n--- glimpse(): every column, its type, and the start of its values\n")
glimpse(ward_visits)
cat("\n--- summary(): range and missingness per column\n")
print(summary(ward_visits$visits))
cat("\n--- count(): how many rows per category, and are the labels consistent?\n")
print(count(ward_visits, ward))
cat("\nFour looks, four different questions. Do all four before wrangling.\n")
head() shows three plausible rows. glimpse() reports 7 rows and 3 columns with their types. summary() gives a range of 388 to 502, a median of 443, a mean of 447.7 — and 1 NA, which is the line that matters, because it is the only place so far that the missing value has been mentioned. count() returns East 2, North 3, South 2, confirming the labels are consistent.
The mistake this prevents
The mistake is running head() and calling that inspection. The first six rows are usually the tidiest in the file, and every problem worth finding — missingness, impossible values, inconsistent labels — lives further down.
Takeaway
Run all four before wrangling. Pay particular attention to the NA count in summary() and to unexpected categories in count(), since both change what your later code has to handle.
