I have a vector with NA
values that I would like to replace by a new factor level NA
.
a = as.factor(as.character(c(1, 1, 2, 2, 3, NA)))
a
[1] 1 1 2 2 3 <NA>
Levels: 1 2 3
This works, but it seems like a strange way to do it.
a = as.factor(ifelse(is.na(a), "NA", a))
class(a)
[1] "factor"
This is the expected output:
a
[1] 1 1 2 2 3 NA
Levels: 1 2 3 NA
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…