EDAV icon indicating copy to clipboard operation
EDAV copied to clipboard

Dealing with NAs as a Factor Level

Open mbmackenzie opened this issue 6 years ago • 1 comments

In the last class we discussed dealing with NA's by replacing and adding the string "NA" as a factor level. There is a function, forcats::fct_explicit_na, that handles this without having to create a new dataframe that might be worth adding to edav.info.

The function takes as an argument na_level, a string to rename the missing level.

For example, to move the NA bar to the bottom:

df <- data.frame(temperature = factor(c("A", "B", "C", NA)), 
                 count = c(60, 50, 20, 40))

ggplot(df, aes(x = fct_relevel(fct_rev(fct_explicit_na(temperature, "NA")), "NA"), 
               y = count)) +
         geom_col() +
         coord_flip()

mbmackenzie avatar Sep 25 '19 07:09 mbmackenzie

This is great, thanks for finding this function! The forcats functions aren't in edav.info yet, but will be sure to include this when they are added. And I will share with the class tonight.

jtr13 avatar Sep 25 '19 12:09 jtr13