Descriptive Statistics in R


Descriptive Statistics in R

Corey
Welcome to Data Science Live. Today we’re working in R with summary functions, also called descriptive statistics: calculating a mean, finding a maximum, and counting records. These are some of the most commonly used statistics in organizations, because questions like “What’s the average?” and “How many records are there?” come up everywhere.

Anna
And the focus is on how we do those calculations in a data frame. In this workflow, we select or refer to columns, then summarize the values down those columns. We’ll use the OECD fertility data set to make that concrete.

Corey
First, the data needs a little preparation. The Excel file has extra header rows and footers, so the example uses `readxl` to read it while skipping the first three rows and limiting the import to 42 records. The original column names aren’t very useful, so the second column is renamed `country`.

Anna
Then `dplyr` helps us keep the columns we want: `country` and the decade columns ending in zero—1970, 1980, 1990, 2000, and 2010. The result has 42 observations and six variables. That’s a useful check: one country column plus five numeric fertility-rate columns.

Corey
The first question is: what was the maximum fertility rate in 2000? With a pipe from the data frame into `summarize()`, we can create a result such as `max_rate = max([2000)`. Because the column name begins with a number, R code refers to it using backticks: `` `2000` ``.

Anna
That summary returns one observation and one variable: the maximum value, about 3.12. But there’s a limitation: a maximum by itself doesn’t tell us which country had that value. It answers “How high?” but not “Where?”

Corey
To identify the country, we can use that maximum inside `filter()`: keep rows where the 2000 column equals the maximum of the 2000 column. That returns the matching record, and in this data the value around 3.12 belongs to India. It’s a nice example of using a summary function as part of a filter condition.

Anna
Next, let’s calculate the average for 2000. Again, `summarize()` creates one value, this time with `mean()` applied to the 2000 column. The reported average fertility rate is about 1.76.

Corey
That mean gives us a useful benchmark, but it usually won’t equal one of the individual country values. So filtering for countries where the 2000 rate equals the mean probably won’t return a useful match. Instead, we can compare each country’s value with the average.

Anna
For example, filter for countries whose 2000 value is less than the mean of that column, then arrange the results by the 2000 value. In the example, 26 of the 42 countries are below the average. Sorting makes it easier to inspect the lower values; the Czech Republic and Russia are among the countries noted as having low rates.

Corey
The last calculation is a record count. We can select the 2000 column and use `summarize(frequency_count = n())`. The `n()` function counts the records in the data being summarized, giving 42 here.

Anna
So we’ve used three core summaries: `max()` for the highest value, `mean()` for the average, and `n()` for the number of records. We also saw that `filter()` can use a maximum or mean to find records relative to a summary, and `arrange()` can order those results.

Corey
One key idea to remember is that these operations summarize down columns in this workflow, rather than across rows. The speaker notes that tidyverse support for summarizing across rows was still being worked on at the time. For now, keep your attention on which column you’re summarizing and what the resulting single value tells you.

Anna
And fertility rates are more than just numbers in a table: the episode points out that long-term demographic trends can be useful for policy outlooks and forecasting. Practice the code with the data, check your column names and results, and keep learning—sometimes trying to answer a question, like which country had the maximum, teaches you a useful new way to combine functions.

Ask anything
We've read this whole study set, so questions about it get the best answers.