Summary statistics
Summary statistics
Once you can pull a column out with $, summarising it takes one function call. These are the ones you will use most:
| R | Python (NumPy) | Returns |
|---|---|---|
mean(x) |
np.mean(x) |
the mean |
median(x) |
np.median(x) |
the median |
sd(x) |
np.std(x, ddof=1) |
the sample standard deviation |
min(x), max(x) |
np.min(x), np.max(x) |
the smallest and largest values |
summary(x) |
— | minimum, quartiles, median, mean and maximum in one go |
Note that sd() uses the sample formula (dividing by n − 1), whereas NumPy’s default divides by n.
The chunk below loads the data each time you open this page.
Mean and standard deviation
Calculate the mean and standard deviation of tmax_c, the monthly maximum temperature.
mean(weather$tmax_c)
sd(weather$tmax_c)The mean is about 12.5 °C and the standard deviation about 5.1 °C. Is that a big spread? It mostly reflects the seasons: winter months are much colder than summer ones.
Extremes
What were the lowest and highest monthly rainfall totals on record? Use min() and max().
min(weather$rain_mm)
max(weather$rain_mm)summary()
summary() gives the minimum, quartiles, median, mean and maximum all at once. Try it on rain_mm. Then try summary(weather), which summarises every column.
summary(weather$rain_mm)
summary(weather)Picking out rows
Often you want a summary of just some of the rows: July only, say. A comparison such as weather$month == 7 returns a logical vector, TRUE where the month is July and FALSE elsewhere. Putting that vector inside square brackets keeps only the TRUE values:
weather$tmax_c[weather$month == 7]Calculate the mean July maximum temperature. Then do the same for January.
mean(weather$tmax_c[weather$month == 7])
mean(weather$tmax_c[weather$month == 1])Quick check
Missing values
This dataset has no gaps, but real data often do. R marks a missing value as NA, and any calculation involving NA returns NA: mean(c(1, 2, NA)) is NA. To skip missing values, add na.rm = TRUE: mean(c(1, 2, NA), na.rm = TRUE) is 1.5. Try it.
mean(c(1, 2, NA), na.rm = TRUE)