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