I'm trying to add the column and row percentage (simultaneously) to a contingency table.
I tried something like this:
library(tidyverse)
library(janitor)
mtcars %>%
tabyl(gear, cyl) %>%
adorn_totals("row", name = "Column Total") %>%
adorn_totals("col") %>%
adorn_percentages("col") %>%
adorn_pct_formatting() %>%
adorn_ns()
gear 4 6 8 Total
3 9.1% (1) 28.6% (2) 85.7% (12) 46.9% (15)
4 72.7% (8) 57.1% (4) 0.0% (0) 37.5% (12)
5 18.2% (2) 14.3% (1) 14.3% (2) 15.6% (5)
Column Total 100.0% (11) 100.0% (7) 100.0% (14) 100.0% (32)
But I only get the column total (each column sums 100%). However I would like to have the row percentage at the same time. In other words, each cell would contain the N, the column percentage and the row percentage, although (eventually) I could sacrifice the N.
janitoris designed for quite simple tables, so (unless I'm not mistaken) in order to create this we have to use other toolsI wasn't sure what format you wanted, so I went with n, row%, col%, but you can change it if you like (just adjust the last mutate call)