R how to change one of the level to NA

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

    Or simply use the inbuilt exclude option, which works regardless of whether the initial variable is a character or factor.

    x <- c("a", "b", "c", "NotPerformed")
    
    factor(x, exclude = "NotPerformed")
    [1] a    b    c    <NA>
    Levels: a b c
    
    factor(factor(x), exclude = "NotPerformed")
    [1] a    b    c    <NA>
    Levels: a b c
    
    0 讨论(0)
  • 2021-01-01 17:07

    Set one of the levels to NA through tidyverse Pipeline, %>%.
    This may serve better as a comment, but I do not have that many reputation.
    In my case, the income variable is int with values of c(1:7, 9). Among the levels, "9" represents "Do not wish to answer".

    ## when all int should be fctr 
    New_data <- data %>% mutate_if(is.integer, as.factor)  %>% 
    mutate(income = fct_recode(income, NULL = "9"))
    

    I also tried recode(), it does not work.

    0 讨论(0)
  • 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    <NA>
    ## Levels: a b c
    

    Note that the factor level is removed.

    0 讨论(0)
  • 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    <NA>
    # Levels: a b c
    
    0 讨论(0)
提交回复
热议问题