Search code examples
rgroupingcolumnsortinggroup

Adding unique ID column associated to two groups R


I have a data frame in this format:

Group Observation
a 1
a 2
a 3
b 4
b 5
c 6
c 7
c 8

I want to create a unique ID column which considers both group and each unique observation within it, so that it is formatted like so:

Group Observation Unique_ID
a 1 1.1
a 2 1.2
a 3 1.3
b 4 2.1
b 5 2.2
c 6 3.1
c 7 3.2
c 8 3.3

Does anyone know of any syntax or functions to accomplish this? The formatting does not need to exactly match '1.1' as long as it signifies group and each unique observation within it. Thanks in advance


Solution

  • Another way using cur_group_id and row_number

    library(dplyr)
    
    A <- 'Group Observation
    a   1
    a   2
    a   3
    b   4
    b   5
    c   6
    c   7
    c   8'
    
    df <- read.table(textConnection(A), header = TRUE)
    
    df |> 
      group_by(Group) |> 
      mutate(Unique_ID = paste0(cur_group_id(), ".", row_number())) |> 
      ungroup()
    
      Group Observation Unique_ID
      <chr>       <int> <chr>    
    1 a               1 1.1      
    2 a               2 1.2      
    3 a               3 1.3      
    4 b               4 2.1      
    5 b               5 2.2      
    6 c               6 3.1      
    7 c               7 3.2      
    8 c               8 3.3