How to create a consecutive group number

后端 未结 8 1090
深忆病人
深忆病人 2020-11-21 16:13

I have a data frame (all_data) in which I have a list of sites (1... to n) and their scores e.g.

  site  score
     1    10
     1    11  
              


        
8条回答
  •  醉酒成梦
    2020-11-21 16:38

    Another way to do it. That I think is easy to get even when you know little about R:

    library(dplyr)
    df <- data.frame('site' = c(1, 1, 1, 4, 4, 4, 8, 8, 8))
    df <- mutate(df, 'number' = cumsum(site != lag(site, default=-1)))
    

提交回复
热议问题