dplyr::group_by() and dplyr::ungroup()
   get_help() docs


Description

The group_by() and ungroup() functions are part of the {dplyr} package, which is part of the {tidyverse}.

We use this function to establish groupings of rows based on given columns for subsequent operations, and it is often (but not only!) used with the {dplyr} package summarize(). After you are done with your grouped operations, it is often a good idea use ungroup() (no arguments needed) to remove groupings and avoid unintended consequences of grouped data.

To use this function, you need to either first load the {dplyr} library, or always use the function with dplyr::group_by() notation.

# Load the library
library(dplyr)
# Or, load the full tidyverse:
library(tidyverse)

# Or, use :: notation
dplyr::group_by()
dplyr::ungroup()

Conceptual Usage

tibble %>% 
  group_by(group subsequent steps on this column)

tibble %>% 
  group_by(group, subsequent, steps, on, these, columns)

tibble that was previously grouped %>% 
  ungroup() # no arguments needed

Examples

The examples below use the msleep dataset. Learn more about this dataset with get_help("msleep").

# Show the msleep dataset with head()
head(msleep)
## # A tibble: 6 × 11
##   name  genus vore  order conservation sleep_total sleep_rem sleep_cycle awake  brainwt  bodywt
##   <chr> <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>    <dbl>   <dbl>
## 1 Owl … Aotus omni  Prim… <NA>                17         1.8      NA       7    0.0155    0.48 
## 2 Moun… Aplo… herbi Rode… nt                  14.4       2.4      NA       9.6 NA         1.35 
## 3 Grea… Blar… omni  Sori… lc                  14.9       2.3       0.133   9.1  0.00029   0.019
## 4 Cow   Bos   herbi Arti… domesticated         4         0.7       0.667  20    0.423   600    
## 5 Thre… Brad… herbi Pilo… <NA>                14.4       2.2       0.767   9.6 NA         3.85 
## 6 Nort… Call… carni Carn… vu                   8.7       1.4       0.383  15.3 NA        20.5


# Calculate the mean time spent awake for each vore group and ungroup when finished
msleep %>% 
  group_by(vore) %>%
  summarize(mean_awake = mean(awake)) %>%
  ungroup()
## # A tibble: 5 × 2
##   vore    mean_awake
##   <chr>        <dbl>
## 1 carni        14.3 
## 2 herbi        14.9 
## 3 insecti       7.48
## 4 omni         13.0 
## 5 <NA>         13.4