R how to change one of the level to NA
Solution 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.
Solution 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
Solution 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
Solution 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.
Related videos on Youtube
lolibility
Updated on June 06, 2022Comments
-
lolibility about 2 years
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? -
thelatemail almost 10 yearsAs a side-note, while
x[x=="NotPerformed"] <- NA
will replace the values withNA
, it will not remove the'NotPerformed'
factor level. Which is why this method is preferable. -
Phil about 7 yearsThis can be updated to include the
forcats
package:fct_recode(x, NULL = "NotPerformed")
.