Package: dplyr


Function: count()


1. Calculate the percent of students per frl group

Review the data (d5)

# A tibble: 7 x 4
  stu_id   frl gender  race
   <dbl> <dbl>  <dbl> <dbl>
1     30     1      1     1
2     30     2      1     2
3     31     2      3     1
4     32     3      2     4
5     33     1      1     3
6     33     3      2     5
7     24     3      4     6
  • Note: The . is used here as a placeholder for d5
d5 %>%
  dplyr::group_by(frl) %>%
  dplyr::summarise(per_group = round(n()/nrow(.)*100, 2))
# A tibble: 3 x 2
    frl per_group
  <dbl>     <dbl>
1     1      28.6
2     2      28.6
3     3      42.9

If you have dplyr 1.1.4 or higher, it is recommended to use reframe() rather than summarise() which has less requirements than summarise() or mutate()

d5 %>%
  dplyr::reframe(per_group = round(n()/nrow(.)*100, 2), .by = frl)

Return to Count