R changes NA to the level in the vector

I have a vector of a data frame that contains about 150,000 lines.

The values ​​of this vector are either "M" or "F" or NA.

So when I ask for levels I got this:

Levels(cards$MaritialStatus)
[1] "M" "S"

      

My goal

I want to scale (normalize) this vector. So I need to change the NA values ​​to the "unknown" word, then I can do the normalization myself.

What I've done:

cards$MaritalStatus[is.na(x = cards$MaritalStatus)] <- "unknown"

      

I got:

Warning message:
In `[<-.factor`(`*tmp*`, is.na(x = cards$MaritalStatus), value = c(1L,  :
  invalid factor level, NA generated

      

However, nothing has changed in my dataframe, I can still see the NA values ​​and when I query the levels of this vector, I still only get "M" and "S".

What have I forgotten, please?

+3


source to share


2 answers


Try the following:



levels(cards$MaritalStatus)<-c(levels(cards$MaritalStatus),"unknown")
cards$MaritalStatus[is.na(cards$MaritalStatus)] <- "unknown"

      

+2


source


Try this solution:



cards <- data.frame(MaritalStatus=c("M","M","S","S",NA,"M","M",NA,"S","S"))

cards$MaritalStatus <- addNA(cards$MaritalStatus)
levels(cards$MaritalStatus)

      

0


source







All Articles