R how to change one of the level to NA

前端 未结 4 638
别跟我提以往
别跟我提以往 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条回答
  •  -上瘾入骨i
    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.

提交回复
热议问题