mercredi 25 septembre 2019

How to fix if else output in terms of assigning NAs to unknown

I'm trying to assign my NAs to "unknown" in my dataset by doing the following code.

str(df$Var)
Factor w/ 4 levels "Current smoker",..: NA NA NA NA NA NA NA 

df$Var<- ifelse(is.na(df$Var),"unknown",df$Var)

After I ran the code the string is changed

str(df$Var
chr [1:100] "unknown" "1" "1"
``

Is there way that I can assign the NAs to "unknown" without losing my variables names to "1","2","3"

I hope that my question is clear

Aucun commentaire:

Enregistrer un commentaire