how to convert factor levels to integer in r

前端 未结 5 470
盖世英雄少女心
盖世英雄少女心 2021-01-21 10:50

I have following dataframe in R

  ID      Season      Year       Weekday
  1       Winter      2017       Monday
  2       Winter      2018       Tuesday
  3             


        
5条回答
  •  猫巷女王i
    2021-01-21 11:15

    We can use match with unique elements

    library(dplyr)
    dat %>%
          mutate_all(funs(match(., unique(.))))
    #   ID Season Year Weekday
    #1  1      1    1       1
    #2  2      1    2       2
    #3  3      2    1       1
    #4  4      2    2       3
    

提交回复
热议问题