I have a logical type column Self_Employed, values as TRUE and FALSE, It has missing values which means say "An Employee" not a self employed person. I would like to impute "Missing" category in the column
class(df$Self_Employed)
[1] "logical"
levels(df$Self_Employed)
NULL
sum(is.na(df$Self_Employed))
[1] 210
table(df$Self_Employed)
FALSE TRUE
1561 271
getting class as "logical", levels as NULL and sum of missing as 210, table shows total of true and false.
To impute missing First I convert to factor, then I impute missing, but not filling up, showing only NA and the levels only saying TRUE and FALSE
df$Self_Employed <- as.factor(df$Self_Employed)
levels(df$Self_Employed)[levels(df$Self_Employed)=="" ] <- "SE_Missing"
levels(df$Self_Employed)
[1] "FALSE" "TRUE"
Levels showing only True and False and is.na shows same 210
df$Self_Employed <- factor(df$Self_Employed,levels=c('FALSE','TRUE',''),labels=c('Yes','No','SE_Missing'))
How to fill the missing factor
I need to convert True to "Yes", False to "No", NA to "SE_Missing"
formating your column first, consider:levels(factor(format(c(TRUE, FALSE, NA))))- MichaelChirico