I have a data frame with 19 variables, 17 of which are factors. Some of these factors contain missing values, coded as NA. I would like to recode missings as a separate factor level "to_impute" using forcats::fct_explicit_na() for all factors in the data frame.
A small example with two factor variables:
df <- structure(list(loc_len = structure(c(NA, NA, NA, NA, NA, NA,
1L, 1L, 3L, 1L), .Label = c("No", "< 5 sec", "5 sec - < 1 min",
"1 - 5 min", "> 5 min", "Unknown duration"), class = "factor"),
AMS = structure(c(1L, 2L, NA, 1L, 1L, NA, NA, NA, NA, NA), .Label = c("No",
"Yes"), class = "factor")), .Names = c("loc_len", "AMS"), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"))
table(df$loc_len, useNA = "always")
No < 5 sec 5 sec - < 1 min 1 - 5 min > 5 min Unknown duration <NA>
3 0 1 0 0 0 6
The code below does this for two variables. I'd like to do this for all factor variables 'f_names' in the data frame. Is there a way to 'vectorize' fct_explicit_na()?
f_names <- names(Filter(is.factor, df))
f_names
[1] "loc_len" "AMS"
The code below does what I want, but separately for each factor:
df_new <- df %>%
mutate(loc_len = fct_explicit_na(loc_len, na_level = "to_impute")) %>%
mutate(AMS = fct_explicit_na(AMS, na_level = "to_impute"))
I'd like tables of this sort for all factors in the dataset, names in 'f_names' :
lapply(df_new, function(x) data.frame(table(x, useNA = "always")))
Now is:
$loc_len
x Freq
1 No 3
2 < 5 sec 0
3 5 sec - < 1 min 1
4 1 - 5 min 0
5 > 5 min 0
6 Unknown duration 0
7 to_impute 6
8 <NA> 0
$AMS
x Freq
1 No 3
2 Yes 1
3 to_impute 6
4 <NA> 0