Reputation: 23
I have tried this, but it did not replace "< NA >" (no spaces)
cwm$v1 <- factor(cwm$v2)
replace(cwm$v1, cwm$v1=="< NA >", 9)
Again with no spaces. It gives me back the variable still with "< NA >"
Upvotes: 0
Views: 57
Reputation: 108623
The symbol <NA>
is a missing value in a factor:
> factor(c("a",NA,"b"))
[1] a <NA> b
Levels: a b
Hence you can simply use is.na
like for all missing values, but you likely see this:
> v1 <- factor(c("a", NA,"b"))
> v1[is.na(v1)] <- 9
> v1
[1] a <NA> b
Levels: a b
Warning message:
In `[<-.factor`(`*tmp*`, is.na(v1), value = 9) :
invalid factor level, NA generated
The reason is simple: 9 is not a level of that factor. So in order to get this done, you need to do this replacement BEFORE you convert to a factor:
> v2 <- c("a",NA,"b")
> v2[is.na(v2)] <- 9
> v1 <- factor(v2)
> v1
[1] a 9 b
Levels: 9 a b
Now you have 9 as a separate level. If you don't want this, you shouldn't use a factor in the first place. So in case you start from a factor, convert to character
first:
> v1 <- factor(c("a", NA,"b"))
> v1 <- as.character(v1)
> v1[is.na(v1)] <- 9
> v1
[1] "a" "9" "b"
Upvotes: 2