Package: dplyr


Function: count()


1. Count how many duplicates we have per student in our data

Review the data (d4)

# A tibble: 7 x 2
  stu_id  year
   <dbl> <dbl>
1     30     1
2     30     2
3     31     2
4     32     3
5     33     1
6     33     3
7     24     3

In this case, a student can be re-recruited back into our study each year. We now want to count how many years each student has been in our study.

In order to do this we can first group by stu_id using dplyr::group_by() and then do a count.

d4 %>%
  dplyr::group_by(stu_id) %>%
  dplyr::count()
# A tibble: 5 x 2
# Groups:   stu_id [5]
  stu_id     n
   <dbl> <int>
1     24     1
2     30     2
3     31     1
4     32     1
5     33     2

Return to Count