Home > Back-end >  Can you create a different grouping variable for each duplicate?
Can you create a different grouping variable for each duplicate?

Time:06-10

I'd like to make a separate grouping variable for each duplicate like so. Does anyone have any suggestions on how to do this?

    without_group<-data.frame(V1= c(1,2,3,1,2,3))

    with_group<- 
    data.frame(
    V1= c("A","A","A","B","B","B"),
    V2= c(1,2,3,1,2,3)
    )

CodePudding user response:

If you are fine with using numbers:

library(dplyr)

without_group %>% group_by(V1) %>% 
  mutate(V2 = row_number())

otherwise if you want to use letters:

without_group %>% group_by(V1) %>% 
  mutate(V2 = LETTERS[row_number()])
  • Related