R: How do I stop the adorn functions in janitor from altering a secondary character column?

736 Views Asked by At

Below is an example of what I'm experiencing

library(tidyverse)
library(janitor)

x <- data_frame(c1=c('a','a','b','b'),
                c2=c('a','b','c','d'),
                c3=runif(4,0,10),
                c4=runif(4,10,20),
                c5=runif(4,20,100))

x %>%
  adorn_totals(c('col')) %>%
  adorn_percentages('row') %>%
  adorn_pct_formatting() %>%
  adorn_ns('front')

Running this code provides the following output:

 c1    c2                c3               c4               c5              Total
  a a (a) 1.0495149  (1.2%) 12.78693 (15.0%) 71.48728 (83.8%)  85.32373 (100.0%)
  a b (b) 0.9217471  (0.9%) 19.71064 (20.1%) 77.67327 (79.0%)  98.30566 (100.0%)
  b c (c) 9.0695540 (19.4%) 14.39917 (30.8%) 23.32479 (49.8%)  46.79351 (100.0%)
  b d (d) 8.9398517  (7.8%) 18.53542 (16.1%) 87.43437 (76.1%) 114.90965 (100.0%)

As you can see, column c2 is just another character column and thus shouldn't be impacted by the adorn_ functions, but as I go from adorn_pct_formatting to adorn_ns it mutates that column in an unexpected way.

Does anyone know how to prevent this while maintaining my same general approach below; meaning that I continue to use the adorn_ functions from janitor?

I'm aware and capable of writing code to calculate and paste the N's and %s into each specific column manually but I'm looking specifically for a way to do it using the functions provided here, if possible.

Thank you.

2

There are 2 best solutions below

0
hisspott On

I was unable to find a mechanism to prevent Janitor adorning the character columns.

There is also a bug logged on the Janitor repo where the adornment of character columns is discussed.

My solution was to process the character column after the adorn function byt splitting the string using stringr::strsplit.

library(stringr)

df %>% 
mutate(character_col = sapply(strsplit(character_col, "\\s\\(" ), [, 1)) 

I hope this is useful to others who come across this issue.

0
Sam Firke On

This is now fixed in janitor v2.0.0. Character columns are left as-is by adorn_ns():

x %>%
    adorn_totals(c('col')) %>%
    adorn_percentages('row') %>%
    adorn_pct_formatting() %>%
    adorn_ns('front')

 c1 c2               c3               c4               c5             Total
  a  a 1.294509  (1.5%) 10.60898 (12.5%) 72.87039 (86.0%) 84.77388 (100.0%)
  a  b 6.557902 (11.2%) 19.27799 (32.9%) 32.73041 (55.9%) 58.56630 (100.0%)
  b  c 5.336008  (7.8%) 17.68432 (25.9%) 45.20563 (66.3%) 68.22596 (100.0%)
  b  d 4.984140  (8.6%) 16.18887 (27.8%) 37.05183 (63.6%) 58.22485 (100.0%)

You can also control which columns to adorn with the ... argument.