R dplyr Message – summarise has grouped output – You can override using groups argument (2 Examples)

In this tutorial, I’ll show how to handle the dplyr message “`summarise()` has grouped output by ‘gr1’. You can override using the `.groups` argument.” in R.

Setting up the Examples

df <- data.frame(A = rep(LETTERS[1:4],     # Constructing an example data frame
                         each = 3),
                 B = letters[1:2],
                 C = 101:112)
df                                         # Showing the data frame in the RStudio console
#    A B   C
# 1  A a 101
# 2  A b 102
# 3  A a 103
# 4  B b 104
# 5  B a 105
# 6  B b 106
# 7  C a 107
# 8  C b 108
# 9  C a 109
# 10 D b 110
# 11 D a 111
# 12 D b 112
install.packages("dplyr")                  # Install dplyr package
library("dplyr")                           # Load dplyr

Example 1: Replicating the Message – `summarise()` has grouped output by ‘X’

df %>%                                     # Grouping the data
  group_by(A, B) %>%
  dplyr::summarise(gr_sum = mean(C))
      # # A tibble: 8 × 3
# # Groups:   A [4]
#   A     B     gr_sum
#   <chr> <chr>  <dbl>
# 1 A     a        102
# 2 A     b        102
# 3 B     a        105
# 4 B     b        105
# 5 C     a        108
# 6 C     b        108
# 7 D     a        111
# 8 D     b        111
# `summarise()` has grouped output by 'A'. You can override using the `.groups` argument.

Example 2: Disabling the Message – `summarise()` has grouped output by ‘X’

options(dplyr.summarise.inform = FALSE)    # Change global options
df %>%                                     # Grouping the data
  group_by(A, B) %>%
  dplyr::summarise(gr_sum = mean(C))
   # # A tibble: 8 × 3
# # Groups:   A [4]
#   A     B     gr_sum
#   <chr> <chr>  <dbl>
# 1 A     a        102
# 2 A     b        102
# 3 B     a        105
# 4 B     b        105
# 5 C     a        108
# 6 C     b        108
# 7 D     a        111
# 8 D     b        111

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top