How to recode an "empty" factor level

Hello. I was wondering if I could ask how to recode a factor that is either names null or empty. I have some data with 4 responses, "", "healthy", "not_healthy", and "NA". I want to recategorize "" and "NA" as "healthy". I was able to do the latter, but not the former.

> aa <- data.wide_all[1:10000, ]
> fct_count(aa$health_status, prop = TRUE)
# A tibble: 4 x 3
  f                 n      p
  <fct>         <int>  <dbl>
1 ""              860 0.086 
2 "healthy"      6393 0.639 
3 "not_healthy"  2435 0.244 
4  NA             312 0.0312
> aa$health_status <- fct_explicit_na(aa$health_status, na_level = "healthy")
> fct_count(aa$health_status, prop = TRUE)
# A tibble: 3 x 3
  f                 n     p
  <fct>         <int> <dbl>
1 ""              860 0.086
2 "healthy"      6705 0.670
3 "not_healthy"  2435 0.244

This solution did not work

aa$health_status<- factor(recode(aa$health_status, NULL = 'healthy'))

nor did calling it "", "NULL", or using a period. Any help would be appreciated!

You could do:

aa$health_status = with(aa, droplevels(replace(health_status, is.na(health_status) | health_status=="", "healthy"))

The replace function takes care of the recoding. After the replacement, the empty string is still one of
the levels of health_status, so we remove it with droplevels.

1 Like

Hi Joel. Thanks so much. This worked perfectly after adding one more ")" at the end. I appreciate it.

Sorry, guess I neglected to close a parenthesis.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.