Question

I have a data frame, df, and a factor class vector, "EMAIL_STATUS". if I do:

table(df$EMAIL_STATUS, useNA="always")

I get 38716 <NA>, 638 "YES", 110 "9999".

I want to convert the 38716 s to "UNKNOWN". I try the following code:

df$EMAIL_STATUS[is.na(df$EMAIL_STATUS)] <- "UNKNOWN"

I get no error, but it does not convert the NAs to "UNKNOWN", in fact it does nothing.

Was it helpful?

Solution

This short example illustrates one of possible ways of introducing a new level into a factor:

x <- factor(c(NA, NA, "a", "b", NA, "b"))
x[is.na(x)] <- "c" # this won't work, no such level as "c" in levels(x)
## Warning message:
## In `[<-.factor`(`*tmp*`, is.na(x), value = "c") :
##   invalid factor level, NA generated
levels(x) <- c(levels(x), "c") #include a new category
x[is.na(x)] <- "c"
x
## [1] c c a b c b

OTHER TIPS

Hard to say without example data but try this

df$EMAIL_STATUS <- as.character(df$EMAIL_STATUS)   
df[ df$EMAIL_STATUS %in% NA, "EMAIL_STATUS" ] <- "UNKNOWN"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top