1. Home
  2. Explore
  3. Statistics
  4. Descriptive Statistics in R

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Descriptive Statistics in R

Summary Functions and Descriptive Statistics in R

Descriptive statistics

Descriptive statistics summarize the values in a dataset. They are commonly used in education, government, and private industry because they provide straightforward answers to questions such as:

  • What is the average value?

  • What is the highest value?

  • How many records are in the dataset?

In R, these operations are often performed on columns in a data frame. A summary may reduce many rows to a single result, while a filter can use a summary value to identify the records that meet a condition.


Preparing the OECD fertility data

The example uses an OECD fertility dataset containing 42 country records and fertility values for five decades: 1970, 1980, 1990, 2000, and 2010. The original spreadsheet includes extra header and footer rows, so it needs to be imported and cleaned before analysis.

The readxl package can import an Excel file, while dplyr provides functions for selecting and transforming data.

library(dplyr)
library(readxl)

Importing and cleaning the data

When importing the spreadsheet:

  • Skip the first three rows if they contain spreadsheet headings rather than data.

  • Limit the import to the 42 data rows so footer information is excluded.

  • Give the country column a meaningful name.

  • Select the country column and the decade columns.

The source example reads the file from a local download because reading the Excel file directly from its URL produced an error.

oecd <- read_xls(
  "path/to/downloaded-file.xls",
  skip = 3,
  n_max = 42
)

The exact column names created during import can depend on the spreadsheet. Once the country and decade columns have been identified, they can be selected for analysis. The ends_with("0") helper selects column names ending in 0, which matches decade names such as 1970, 1980, and 2000.

oecd <- oecd %>%
  rename(country = 2) %>%
  select(country, ends_with("0"))

After cleaning, the dataset should have 42 observations and six variables: one country column and five decade columns. The decade columns contain numeric values, making them suitable for numerical summaries.

Check the data before summarizing it: Confirm that the intended rows and columns were imported and that the values to be summarized are numeric.


Using summarise() to calculate a maximum

The summarise() function computes a summary from a column. It typically reduces a dataset to one row for each group being summarized; when no groups are specified, it produces one row for the whole dataset.

To find the maximum fertility rate in 2000, apply max() to the 2000 column:

max_2000 <- oecd %>%
  summarise(max_rate = max(`2000`))

Column names that begin with a number must be enclosed in backticks when referenced in R code. The result is one value, reported in the example as approximately 3.12.

A maximum alone does not identify which country had that value. To find the corresponding record, use the maximum as a condition in filter():

max_2000_country <- oecd %>%
  filter(`2000` == max(`2000`))

The filtered result retains the original columns, allowing the country associated with the maximum to be identified. In the example, the maximum value of about 3.12 belongs to India.

This illustrates a useful distinction:

  • summarise() returns a compact result, such as the maximum value.

  • filter() retains rows that meet a condition, such as the row containing that maximum.


Using summarise() to calculate a mean

The mean is the arithmetic average of a set of values. To calculate the average fertility rate across the countries in 2000, apply mean() to that column:

average_2000 <- oecd %>%
  summarise(average_rate = mean(`2000`))

The example gives an average of approximately 1.76. Because this is a summary of the entire column, it is a single value rather than a separate value for each country.

Finding values below the mean

The mean can be used as a threshold in filter(). To keep countries whose 2000 value is below the overall average, compare each row’s value with the column mean:

below_average <- oecd %>%
  filter(`2000` < mean(`2000`)) %>%
  arrange(`2000`)

filter() retains the records whose values satisfy the condition. arrange() sorts those records by the 2000 value in ascending order, placing the lowest values first.

In the example, 26 of the 42 countries are below the average for 2000. The sorted output makes it easier to inspect the lowest values and compare countries.


Counting records

The n() function counts the number of rows in the current data. It is commonly used inside summarise():

record_count <- oecd %>%
  select(`2000`) %>%
  summarise(frequency_count = n())

The result is 42, matching the number of country records in the dataset. In this example, selecting the 2000 column first makes clear which data are being examined, but n() counts rows; it does not calculate the number of non-missing values in that column.


Key distinctions and practical considerations

  • Maximum: max(column) returns the largest value.

  • Mean: mean(column) returns the arithmetic average.

  • Row count: n() returns the number of rows in the data being summarized.

  • Summary versus filtering: summarise() produces summary output; filter() selects records that meet a condition.

  • Column-oriented operations: These examples summarize values down columns, rather than calculating summaries across the values in each row.

  • Missing values: In base R, max() and mean() generally return NA if the column contains missing values unless missing values are explicitly removed, for example with max(column, na.rm = TRUE) or mean(column, na.rm = TRUE).

Descriptive summaries are useful for quickly understanding a dataset and for answering common analytical questions. The same patterns can be adapted to other numerical columns, datasets, and summary measures.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent