R how to change one of the level to NA

前端 未结 4 690
别跟我提以往
别跟我提以往 2021-01-01 16:20

I have a data set and one of its column has factor levels \"a\" \"b\" \"c\" \"NotPerformed\". How can I change all the \"NotPerformed\" factors to

4条回答
  •  醉话见心
    2021-01-01 17:14

    I revise my old answer and provide what you can do as of September 2016. With the development of the dplyr package, now you can use recode_factor() to do the job.

    x <- factor(c("a", "b", "c", "NotPerformed"))
    
    # [1] a            b            c            NotPerformed
    # Levels: a b c NotPerformed
    
    library(dplyr)
    recode_factor(x, NotPerformed = NA_character_)
    # [1] a    b    c    
    # Levels: a b c
    

提交回复
热议问题