How to create a consecutive group number

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

    Another solution using the data.table package.

    Example with the more complete datset provided by Jaap:

    setDT(dat)[, number := frank(site, ties.method = "dense")]
    dat
        site     score number
     1:    1 0.3107920      1
     2:    1 0.3640102      1
     3:    1 0.1715318      1
     4:    8 0.7247535      3
     5:    8 0.1263025      3
     6:    8 0.4657868      3
     7:    4 0.6915818      2
     8:    4 0.3558270      2
     9:    4 0.3376173      2
    10:    8 0.7934963      3
    11:    8 0.9641918      3
    12:    8 0.9832120      3
    

提交回复
热议问题