sfirke/janitor

Add `adorn_totals` as level in existing factor variable

themichjam opened this issue · 4 comments

Is there a way of adding the total called via adorn_totals as a new level to an existing factor? E.G. Adding the Total row produced by adorn_totals() as a factor level to the starwars sex variable?

Repex below:

# starwars data
data(starwars)

# gender 
starwars %>%
  count(sex)

# A tibble: 5 × 2
  sex                n
  <chr>          <int>
1 female            16
2 hermaphroditic     1
3 male              60
4 none               6
5 NA                 4

# make fct
starwars$sex <- as.factor(as.character(starwars$sex))

# with totals
starwars %>%
  count(sex) %>%
  adorn_totals()

            sex  n
         female 16
 hermaphroditic  1
           male 60
           none  6
           <NA>  4
          Total 87

At first glance this looks like a good idea, thank you. That is, if the variable (or col variable in a 2-way table) comes in as a factor, it should leave as a factor with a new level Total (or whatever the use specifies).

It shouldn't be very difficult to implement, but not trivial either. I haven't been doing much enhancing of janitor, just routine maintenance, so not sure when or if I'll get to it. But I like the idea.

Thank you. Is there a workaround to implement this in the meantime?

implemented this on main!