Search code examples
rtidyversecrosstab

crosstab variables of tibble and make the output readable


I have to cross tabulate variables of tibble. I used table() for it, but the output is not easily readable.

Is there a way to format the output to make it more easily readable.

Thanks

library(tidyverse)

# random arrays of 0 and 1
a <- sample(c(0, 1, 2, 3, 4, 5), replace = TRUE, size = 100)
b <- sample(c(0, 1, 2, 3, 4, 5), replace = TRUE, size = 100)

tbl <- tibble(a, b)

cross_tab <- table(tbl$a, tbl$b)

cross_tab

Solution

  • I use expss for these kinds of tables:

    library(expss)
    cro(tbl$a,tbl$b) %>% htmlTable()
    

    You can precede the command above with the expss command apply_labels to format the variable and value names. See the documentation for details.