George Tarr
George Tarr

Reputation: 23

How to replace <NA> with a number for a single factor variable in R?

Again with no spaces. It gives me back the variable still with "< NA >"

Upvotes: 0

Views: 57

Answers (1)

Joris Meys
Joris Meys

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

Related Questions