How to create a consecutive group number

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

    You can turn site into a factor and then return the numeric or integer values of that factor:

    dat <- data.frame(site = rep(c(1,4,8), each = 3), score = runif(9))
    dat$number <- as.integer(factor(dat$site))
    dat
    
      site     score number
    1    1 0.5305773      1
    2    1 0.9367732      1
    3    1 0.1831554      1
    4    4 0.4068128      2
    5    4 0.3438962      2
    6    4 0.8123883      2
    7    8 0.9122846      3
    8    8 0.2949260      3
    9    8 0.6771526      3
    

提交回复
热议问题