R – Group by variable and then assign a unique ID [duplicate]

dplyr::group_indices() is deprecated as of dplyr 1.0.0. dplyr::cur_group_id() should be used instead:

df %>%
 group_by(personal_id) %>%
 mutate(group_id = cur_group_id())

  personal_id gender temperature group_id
  <chr>       <chr>        <dbl>    <int>
1 111-11-1111 M             99.6        1
2 999-999-999 F             98.2        3
3 111-11-1111 M             97.8        1
4 999-999-999 F             98.3        3
5 888-88-8888 F             99          2
6 111-11-1111 M             98.9        1

Leave a Comment