rcrosstable

How to control percent_pattern in the R Crosstable package?


When using Crosstable in RStudio, I don't want to include a percent result alongside my figures in the total column. I can control the appearance of percentages within the body of my table using percent_pattern = {n}, however this does not remove percentages from my results column.

This:

library(crosstable)
crosstable(mtcars2, am, by=vs, total="both", percent_pattern = "{n}") %>% 
  as_flextable(keep_id=TRUE)

Produces this:

Output

The package news explains the following: percent_pattern can now be a list of characters with names body, total_row, total_col, and total_all to also control the pattern in other parts of the crosstable than the body.

Based on this I have used the code below, however the percentages remain.

crosstable(mtcars2, am, by=vs, total="both", percent_pattern = list(total_all = "{n}")) %>% 
  as_flextable(keep_id=TRUE)

Any ideas?


Solution

  • As you can see in the vignette, the proper way to change percent_pattern in all the different parts of a crosstable is the following:

    pp_n = list(body="{n}", total_row="{n}", total_col="{n}", total_all="{n}")
    crosstable(mtcars2, cyl, by=vs, total=TRUE, 
               percent_pattern=pp_n) %>% 
      as_flextable()
    

    If you want this behavior for all your crosstable, you can change the default pattern through options like this:

    crosstable_options(percent_pattern=pp_n)
    

    EDIT: Your second code is definitely a bug (https://github.com/DanChaltiel/crosstable/issues/52), it will be corrected in the next version (which will be published this century for sure).