R how to change one of the level to NA

前端 未结 4 683
别跟我提以往
别跟我提以往 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:08

    Set the level to NA:

    x <- factor(c("a", "b", "c", "NotPerformed"))
    x
    ## [1] a            b            c            NotPerformed
    ## Levels: a b c NotPerformed
    levels(x)[levels(x)=='NotPerformed'] <- NA
    x
    ## [1] a    b    c    
    ## Levels: a b c
    

    Note that the factor level is removed.

提交回复
热议问题