How to create a consecutive group number

后端 未结 8 1077
深忆病人
深忆病人 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:57

    Using the data from @Jaap, a different dplyr possibility using dense_rank() could be:

    dat %>%
     mutate(ID = dense_rank(site))
    
       site     score ID
    1     1 0.1884490  1
    2     1 0.1087422  1
    3     1 0.7438149  1
    4     8 0.1150771  3
    5     8 0.9978203  3
    6     8 0.7781222  3
    7     4 0.4081830  2
    8     4 0.2782333  2
    9     4 0.9566959  2
    10    8 0.2545320  3
    11    8 0.1201062  3
    12    8 0.5449901  3
    

    Or a rleid()-like dplyr approach, with the data arranged first:

    dat %>%
     arrange(site) %>%
     mutate(ID = with(rle(site), rep(seq_along(lengths), lengths)))
    
       site     score ID
    1     1 0.1884490  1
    2     1 0.1087422  1
    3     1 0.7438149  1
    4     4 0.4081830  2
    5     4 0.2782333  2
    6     4 0.9566959  2
    7     8 0.1150771  3
    8     8 0.9978203  3
    9     8 0.7781222  3
    10    8 0.2545320  3
    11    8 0.1201062  3
    12    8 0.5449901  3
    

    Or using duplicated() and cumsum():

    df %>%
     mutate(ID = cumsum(!duplicated(site)))
    

    The same with base R:

    df$ID <- with(rle(df$site), rep(seq_along(lengths), lengths))
    

    Or:

    df$ID <- cumsum(!duplicated(df$site))
    

提交回复
热议问题