问题
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 NA?
回答1:
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.
回答2:
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
回答3:
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
回答4:
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.
来源:https://stackoverflow.com/questions/25354964/r-how-to-change-one-of-the-level-to-na