R - add column that counts sequentially within groups but repeats for duplicates

前端 未结 3 1040
刺人心
刺人心 2020-12-01 22:49

I\'m looking for a solution to add the column \"desired_result\" preferably using dplyr and/or ave(). See the data frame here, where the group is \"section\" and the unique

3条回答
  •  -上瘾入骨i
    2020-12-01 23:26

    dense_rank it is

    library(dplyr)
    df %>% 
      group_by(section) %>% 
      mutate(desire=dense_rank(exhibit))
    #  section exhibit desired_result desire
    #1       1       a              1      1
    #2       1       b              2      2
    #3       1       c              3      3
    #4       1       c              3      3
    #5       2       a              1      1
    #6       2       b              2      2
    #7       2       b              2      2
    #8       2       c              3      3
    

提交回复
热议问题