R - Group by variable and then assign a unique ID

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

dplyr has a group_indices function for creating unique group IDs

library(dplyr)
data <- data.frame(personal_id = c("111-111-111", "999-999-999", "222-222-222", "111-111-111"),
                       gender = c("M", "F", "M", "M"),
                       temperature = c(99.6, 98.2, 97.8, 95.5))

data$group_id <- data %>% group_indices(personal_id) 
data <- data %>% select(-personal_id)

data
  gender temperature group_id
1      M        99.6        1
2      F        98.2        3
3      M        97.8        2
4      M        95.5        1

Or within the same pipeline (https://github.com/tidyverse/dplyr/issues/2160):

data %>% 
    mutate(group_id = group_indices(., personal_id))

Tags:

R

Dplyr