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

前端 未结 3 802
半阙折子戏
半阙折子戏 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:23

    We can use match to get the index of the unique values, and then add 99

    df1$Stock_Code <- match(df1$Stock_Code, unique(df1$Stock_Code)) + 99
    df1$Stock_Code
    [1] 100 101 102 100 101
    

    Or another option is to convert to factor and coerce to integer

    with(df1, as.integer(factor(Stock_Code, levels = unique(Stock_Code)))+ 99)
    #[1] 100 101 102 100 101
    

提交回复
热议问题