我有一个数据框,df和一个因子类向量," EMAIL_STATUS"。如果我这样做:
table(df$EMAIL_STATUS, useNA="always")
我得到38716 <NA>
,638 "YES"
,110 "9999"
。
我想将38716转换为&#34; UNKNOWN&#34;。我尝试以下代码:
df$EMAIL_STATUS[is.na(df$EMAIL_STATUS)] <- "UNKNOWN"
我没有得到任何错误,但它没有将NAs转换为&#34; UNKNOWN&#34;,实际上它什么也没做。
答案 0 :(得分:8)
这个简短的例子说明了将新的级别引入因子的可能方法之一:
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
答案 1 :(得分:0)
很难说没有示例数据 但试试这个
df$EMAIL_STATUS <- as.character(df$EMAIL_STATUS)
df[ df$EMAIL_STATUS %in% NA, "EMAIL_STATUS" ] <- "UNKNOWN"