Create statistical summaries in R with the summarise() function from dplyr
The summarise (or summarize) function is used for aggregating and summarizing data. It’s particularly helpful for condensing data into a single row per group, offering various statistical summaries or computations for each group. This function creates a new data frame with the specified summary statistics.
Quick answer
Use summarise(data, new_column = function(column)) to collapse a data frame into summary statistics. Combine it with group_by to get one row per group. For instance, the following code calculates the mean mpg and the number of cars for each number of cylinders of the built-in mtcars dataset:
library(dplyr)
mtcars %>%
group_by(cyl) %>%
summarise(mean_mpg = mean(mpg), n = n())
# A tibble: 3 × 3
cyl mean_mpg n
<dbl> <dbl> <int>
1 4 26.7 11
2 6 19.7 7
3 8 15.1 14
Syntax
The summarise or summarize function takes a dataset as input and creates a new one with columns calculated by applying a function to one or multiple columns from the original data. The syntax is as follows:
# Basic usage
summarise(data, new_column = function(column))
summarise(
.data, # Data set
..., # New columns (new_column = function(column))
.by = NULL, # Grouping variables
.groups = NULL # Grouping structure of the result
)
summarise and summarize are both the same function.
summarise vs summarize
There is no difference between summarise and summarize. dplyr provides both spellings (British and American English) and one is just an alias of the other, so they take the same arguments and return the same output:
library(dplyr)
summarise(mtcars, mean_mpg = mean(mpg))
summarize(mtcars, mean_mpg = mean(mpg))
mean_mpg
1 20.09062
mean_mpg
1 20.09062
Be careful if you also load the plyr package, as it has its own summarise and summarize functions. See the common errors section for more details.
Statistical summaries of the data
Given a dataset, you can generate a new data frame containing statistical summaries of specific variables from the original data frame. The table below describes some of the most useful functions for use with summarise, such as mean or sum.
| Function | Description |
|---|---|
| mean() | Mean of the values |
| median() | Median of the values |
| sd(), var() | Standard deviation and variance of the values |
| quantile() | Quantiles of the values |
| IQR() | Interquartile range |
| min(), max() | Minimum and maximum value |
| first() | First value |
| last() | Last value |
| nth() | Nth value |
| n() | Number of elements per group |
| n_distinct() | Number of unique values |
In the example below we demonstrate how to generate a new data frame containing the average of the numerical variables from the original data frame.
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
# Mean of 'x' and mean of 'y'
df_2 <- df %>%
summarise(mean_x = mean(x), mean_y = mean(y))
df_2
mean_x mean_y
1 21.6 38.2

Note that if the function returns more than one value, the output will contain as many rows as values returned. However, since dplyr 1.1.0 this usage of summarise is deprecated and you will get a warning. Use the reframe function instead.
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
# Quartiles of 'x' and quartiles of 'y'
df_2 <- df %>%
summarise(quartiles_x = quantile(x), quartiles_y = quantile(y))
df_2
Warning message:
Returning more (or less) than 1 row per `summarise()` group was deprecated in
dplyr 1.1.0.
ℹ Please use `reframe()` instead.
ℹ When switching from `summarise()` to `reframe()`, remember that `reframe()`
always returns an ungrouped data frame and adjust accordingly.
quartiles_x quartiles_y
1 3 18
2 12 37
3 19 42
4 30 44
5 44 50

Count rows with n() and n_distinct()
The n function returns the number of rows (of the data or of each group) and n_distinct returns the number of unique values of a column. Neither of them needs a column name inside n().
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
# Number of rows and number of different groups
df_2 <- df %>%
summarise(n = n(), n_groups = n_distinct(group))
df_2
n n_groups
1 5 2
Used by group, n() gives the number of observations of each group:
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
# Number of rows of each group
df_2 <- df %>%
summarise(n = n(), .by = group)
df_2
group n
1 G1 3
2 G2 2
Missing values and na.rm
If a column contains missing values, functions like mean, sum or max return NA. In the following example the second value of x is NA, so its mean is also NA:
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
df$x[2] <- NA
# Mean of 'x' and mean of 'y'
df_2 <- df %>%
summarise(mean_x = mean(x), mean_y = mean(y))
df_2
mean_x mean_y
1 NA 38.2
To ignore the missing values, set na.rm = TRUE inside each function. You can also count how many missing values each column has with sum(is.na(x)):
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
df$x[2] <- NA
# Mean of 'x' and mean of 'y' removing NA and number of NA in 'x'
df_2 <- df %>%
summarise(mean_x = mean(x, na.rm = TRUE),
mean_y = mean(y, na.rm = TRUE),
n_na = sum(is.na(x)))
df_2
mean_x mean_y n_na
1 26.25 38.2 1
Check the post about NA values in R to learn more about working with missing data.
Summarise data by group with group_by
The summarise function is particularly useful in conjunction with group_by. In this scenario, the new data frame will contain statistical summaries for each group. In base R, you can obtain similar results with the aggregate function.
The example below calculates the sum of each column based on the groups of the group variable.
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
# Sum of 'x' and sum of 'y' by group
df_2 <- df %>%
group_by(group) %>%
summarise(mean_x = sum(x), mean_y = sum(y))
df_2
# A tibble: 2 × 3
group mean_x mean_y
<chr> <int> <int>
1 G1 52 97
2 G2 56 94

Additionally, you can group by more than one categorical variable. In this scenario, the function calculates statistical summaries for each group and subgroup. By default, the output is grouped by the first categorical variable, as indicated by a message.
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
group_2 = sample(c("G3", "G4"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
# Sum of 'x' and sum of 'y' by group
df_2 <- df %>%
group_by(group, group_2) %>%
summarise(sum_x = sum(x), sum_y = sum(y))
df_2
`summarise()` has grouped output by 'group'. You can override using the `.groups` argument.
# A tibble: 4 × 4
# Groups: group [2]
group group_2 sum_x sum_y
<chr> <chr> <int> <int>
1 G1 G3 74 86
2 G1 G4 18 30
3 G2 G3 48 22
4 G2 G4 37 35

The .groups argument is optional and can take one of the following values: "drop_last" to drop the last level of grouping, "drop" to drop all groups, "keep" to preserve the original grouping or "rowwise", to treat each row as its own group. If you are not sure which one to use, check what the grouped output message means.
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
group_2 = sample(c("G3", "G4"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
# Sum of 'x' and sum of 'y' by group
df_2 <- df %>%
group_by(group, group_2) %>%
summarise(sum_x = sum(x), sum_y = sum(y), .groups = "drop")
df_2
# A tibble: 4 × 4
group group_2 sum_x sum_y
<chr> <chr> <int> <int>
1 G1 G3 74 86
2 G1 G4 18 30
3 G2 G3 48 22
4 G2 G4 37 35
Notice the difference from the previous output:

The .by argument
Since dplyr 1.1.0, summarise has a .by argument that lets you group only for that operation, without calling group_by first. The result is always ungrouped, so you don’t need .groups nor ungroup, and the groups keep the order in which they appear in the data.
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
# Mean of 'x' and mean of 'y' by group
df_2 <- df %>%
summarise(mean_x = mean(x), mean_y = mean(y), .by = group)
df_2
group mean_x mean_y
1 G1 17.33333 32.33333
2 G2 28.00000 47.00000
To group by several variables, pass them inside c(), as in .by = c(group, group_2). Read more about the differences between .by and group_by.
Summarise multiple columns
Instead of manually specifying several columns, you can create summaries by selecting them based on a condition using summarise in combination with across. See the list of helper functions to select columns.
In the following example, the variance of all columns except group is calculated, and the resulting columns are renamed using the original column names with the "_var" suffix.
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
# Summarise all columns by their variance except 'group'
df_2 <- df %>%
summarise(across(!group, var, .names = "{.col}_var"))
df_2
x_var y_var
1 254.3 149.2

The where function is highly useful as it enables the selection of columns based on a condition, like choosing only numeric columns using where(is.numeric).
For example, the following code calculates the median for all numeric variables.
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
# Summarise ALL the NUMERIC columns by their median
df_2 <- df %>%
summarise(across(where(is.numeric), median, .names = "{.col}_median"))
df_2
x_median y_median
1 19 42

Summarise but keep all the columns
summarise only returns the grouping variables and the new columns. If you want to add the summary to each row while keeping all the original columns, use the mutate function instead of summarise. With grouped data, mutate repeats the value of each group in all its rows:
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
# Mean of 'x' by group keeping all the rows and columns
df_2 <- df %>%
mutate(mean_x = mean(x), .by = group)
df_2
group x y mean_x
1 G1 19 37 17.33333
2 G1 3 18 17.33333
3 G2 12 50 28.00000
4 G1 30 42 17.33333
5 G2 44 44 28.00000
Another option is to summarise first and then join the result back to the original data with a left join. This is useful when you want to reuse the summary table:
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
# Summary by group
res <- df %>% summarise(mean_x = mean(x), .by = group)
# Add the summary to the original data
df_2 <- df %>% left_join(res, by = "group")
df_2
group x y mean_x
1 G1 19 37 17.33333
2 G1 3 18 17.33333
3 G2 12 50 28.00000
4 G1 30 42 17.33333
5 G2 44 44 28.00000
If what you need is the full row that has the maximum or minimum value of each group, use slice_max or slice_min instead. See the slice function for more examples.
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
# Row with the maximum value of 'x' of each group
df_2 <- df %>%
group_by(group) %>%
slice_max(x, n = 1) %>%
ungroup()
df_2
# A tibble: 2 × 3
group x y
<chr> <int> <int>
1 G1 30 42
2 G2 44 44
Return several rows per group with reframe
Since dplyr 1.1.0, summarise should return exactly one row per group. If your function returns several values, such as quantile or range, use reframe instead. It works like summarise but allows any number of rows and always returns an ungrouped data frame.
In the example below we calculate the quartiles of x for each group, adding a column with the probability of each quartile:
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
# Quartiles of 'x' by group
df_2 <- df %>%
reframe(prob = c(0.25, 0.5, 0.75),
q_x = quantile(x, prob), .by = group)
df_2
group prob q_x
1 G1 0.25 11.0
2 G1 0.50 19.0
3 G1 0.75 24.5
4 G2 0.25 20.0
5 G2 0.50 28.0
6 G2 0.75 36.0
Learn more about quantiles in R.
Common errors
Warning: returning more (or less) than 1 row per summarise() group was deprecated
This warning appears when the function inside summarise returns more than one value per group:
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
df_2 <- df %>% summarise(range_x = range(x))
Warning message:
Returning more (or less) than 1 row per `summarise()` group was deprecated in
dplyr 1.1.0.
ℹ Please use `reframe()` instead.
ℹ When switching from `summarise()` to `reframe()`, remember that `reframe()`
always returns an ungrouped data frame and adjust accordingly.
The code still works, but you should replace summarise with reframe. If you expected a single value, check the function: for instance, use min(x) and max(x) instead of range(x).
summarise ignores group_by (plyr loaded after dplyr)
If you get a single row instead of one row per group, you probably loaded the plyr package after dplyr (plyr prints a message warning you about it when you load it). In that case, summarise refers to the plyr function, which ignores the groups:
library(dplyr)
library(plyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
df %>% group_by(group) %>% summarise(mean_x = mean(x))
mean_x
1 21.6
To fix it, call the function with its namespace, dplyr::summarise, or load plyr before dplyr:
df %>% group_by(group) %>% dplyr::summarise(mean_x = mean(x))
# A tibble: 2 × 2
group mean_x
<chr> <dbl>
1 G1 17.3
2 G2 28
Warning: argument is not numeric or logical: returning NA
This warning means that you applied a numeric function, like mean, to a character or factor column:
library(dplyr)
set.seed(9)
df <- data.frame(group = sample(c("G1", "G2"), 5, replace = TRUE),
x = sample(1:50, 5), y = sample(1:50, 5))
df %>% summarise(mean_group = mean(group))
mean_group
1 NA
Warning message:
There was 1 warning in `summarise()`.
ℹ In argument: `mean_group = mean(group)`.
Caused by warning in `mean.default()`:
! argument is not numeric or logical: returning NA
Make sure you are summarising the right column or convert it first with as.numeric. To summarise all numeric columns at once, use across(where(is.numeric), mean) as shown above.
The result is NA
If a summary returns NA without any warning, the column contains missing values. Add na.rm = TRUE to the function, as explained in the missing values section.