缺少数据-转换NA在R中不起作用



我有一个数据帧df和一个因子类向量"EMAIL_STATUS"。如果我这样做:

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

我得到了38716 <NA>638 "YES"110 "9999"

我想将38716转换为"未知"。我尝试以下代码:

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

我没有得到任何错误,但它并没有将NA转换为"UNKNOWN",事实上它什么也没做。

这个简短的例子说明了将新的级别引入因子的一种可能方法:

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

没有示例数据很难说但是试试这个

df$EMAIL_STATUS <- as.character(df$EMAIL_STATUS)   
df[ df$EMAIL_STATUS %in% NA, "EMAIL_STATUS" ] <- "UNKNOWN"

最新更新