Assign random values to column according to another column's values in R

前端 未结 3 847
半阙折子戏
半阙折子戏 2021-01-25 03:28

I have a dataset that has Stock Codes with the range from 2-90214 (which has around 3000 unique values). Obviously, some values between 2 and 90214 are getting skipped. I want t

3条回答
  •  半阙折子戏
    2021-01-25 04:21

    We can convert the numbers into factor and then transform it into numeric

    as.numeric(factor(df$StockCode))
    
    #[1] 1 3 2 1 3
    

    If we need it starting from 100 we can add 99 in it

    as.numeric(factor(df$StockCode)) + 99
    

    Same numbers would get same factor level which upon converting into numeric would give same numeric value

提交回复
热议问题