Cumulative counts for repeated measures e.g., infections

survival::cgd %>% 
  group_by(id) %>% 
  mutate(
    infection_number = row_number()) %>% 
  ungroup()
# A tibble: 203 × 2
      id infection_number
   <int>            <int>
 1     1                1
 2     1                2
 3     1                3
 4     2                1
 5     2                2
 6     2                3
 7     2                4
 8     2                5
 9     2                6
10     2                7
# … with 193 more rows

Count the number of distinct elements in a variable

starwars %>% 
  distinct(name) %>% 
  count()
# A tibble: 1 × 1
      n
  <int>
1    87

Count the number of distinct elements in all variables

starwars %>% 
  map_dbl(., n_distinct)
      name     height       mass hair_color skin_color  eye_color birth_year 
        87         46         39         13         31         15         37 
       sex     gender  homeworld    species      films   vehicles  starships 
         5          3         49         38         24         11         17 

Count the NAs per row

airquality %>% 
  rowwise() %>%
    mutate(number_missing = sum(is.na(cur_data()))) %>%
    ungroup()
# A tibble: 153 × 7
   Ozone Solar.R  Wind  Temp Month   Day number_missing
   <int>   <int> <dbl> <int> <int> <int>          <int>
 1    41     190   7.4    67     5     1              0
 2    36     118   8      72     5     2              0
 3    12     149  12.6    74     5     3              0
 4    18     313  11.5    62     5     4              0
 5    NA      NA  14.3    56     5     5              2
 6    28      NA  14.9    66     5     6              1
 7    23     299   8.6    65     5     7              0
 8    19      99  13.8    59     5     8              0
 9     8      19  20.1    61     5     9              0
10    NA     194   8.6    69     5    10              1
# … with 143 more rows

Count the number of NAs in each variable

colSums(is.na(airquality))
  Ozone Solar.R    Wind    Temp   Month     Day 
     37       7       0       0       0       0 

Counting the number and proportions of a categorical variable

starwars %>% 
  count(species) %>% 
  mutate(proportions = n/sum(n))
# A tibble: 38 × 3
   species       n proportions
   <chr>     <int>       <dbl>
 1 Aleena        1      0.0115
 2 Besalisk      1      0.0115
 3 Cerean        1      0.0115
 4 Chagrian      1      0.0115
 5 Clawdite      1      0.0115
 6 Droid         6      0.0690
 7 Dug           1      0.0115
 8 Ewok          1      0.0115
 9 Geonosian     1      0.0115
10 Gungan        3      0.0345
# … with 28 more rows

Counting the number and proportions of all categorical variables

iris %>%
  select(where(is.factor)) %>% 
  map(., ~tabyl(.))
$Species
          .  n   percent
     setosa 50 0.3333333
 versicolor 50 0.3333333
  virginica 50 0.3333333

Count the proportion of missing data in each variable

airquality %>% 
  summarise(across(everything(), ~mean(is.na(.))))
# A tibble: 1 × 6
  Ozone Solar.R  Wind  Temp Month   Day
  <dbl>   <dbl> <dbl> <dbl> <dbl> <dbl>
1 0.242  0.0458     0     0     0     0