Create tidyverse-friendly tables of frequencies
You can install tidytab from GitHub with:
# install.packages("devtools")
devtools::install_github("gvelasq/tidytab")
# setup (tidytab automatically exports the magrittr pipe)
library(tidytab)
# one-way table of frequencies
mtcars %>% tab(cyl)
#> cyl β Freq. Percent Cum.
#> βββββββββββββΌβββββββββββββββββββββββββββββββββββ
#> 4 β 11 34.4 34.4
#> 6 β 7 21.9 56.3
#> 8 β 14 43.8 100.0
#> βββββββββββββΌβββββββββββββββββββββββββββββββββββ
#> Total β 32 100.0 Β
# two-way table of frequencies (a special 2x2 contingency table)
mtcars %>% tab(cyl, gear)
#> β gear β
#> cyl β 3 4 5 β Total
#> ββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββ
#> 4 β 1 8 2 β 11
#> 6 β 2 4 1 β 7
#> 8 β 12 0 2 β 14
#> ββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββ
#> Total β 15 12 5 β 32
# flat contingency tables of three (or more) variables
mtcars %>% tab(cyl, gear, am)
#> cyl β gear am Freq. Percent Cum.
#> βββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> 4 β 3 0 1 3.1 3.1
#> 4 β 4 0 2 6.2 9.3
#> 4 β 4 1 6 18.8 28.1
#> 4 β 5 1 2 6.2 34.3
#> ------------βΌ-----------------------------------------------------------
#> 6 β 3 0 2 6.2 40.5
#> 6 β 4 0 2 6.2 46.7
#> 6 β 4 1 2 6.2 52.9
#> 6 β 5 1 1 3.1 56.0
#> ------------βΌ-----------------------------------------------------------
#> 8 β 3 0 12 37.5 93.5
#> 8 β 5 1 2 6.2 99.7
# tables wider than the R console are automatically wrapped
mtcars %>% tab(cyl, gear, am, vs)
#> cyl β gear am vs Freq. Percent
#> βββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
#> 4 β 3 0 1 1 3.1
#> 4 β 4 0 1 2 6.2
#> 4 β 4 1 1 6 18.8
#> 4 β 5 1 0 1 3.1
#> 4 β 5 1 1 1 3.1
#> ------------βΌ-----------------------------------------------------------
#> 6 β 3 0 1 2 6.2
#> 6 β 4 0 1 2 6.2
#> 6 β 4 1 0 2 6.2
#> 6 β 5 1 0 1 3.1
#> ------------βΌ-----------------------------------------------------------
#> 8 β 3 0 0 12 37.5
#> 8 β 5 1 0 2 6.2
#>
#> cyl β Cum.
#> βββββββββββββΌββββββββββββ
#> 4 β 3.1
#> 4 β 9.3
#> 4 β 28.1
#> 4 β 31.2
#> 4 β 34.3
#> ------------βΌ------------
#> 6 β 40.5
#> 6 β 46.7
#> 6 β 52.9
#> 6 β 56.0
#> ------------βΌ------------
#> 8 β 93.5
#> 8 β 99.7
# missing values are displayed in red
tab(letters[24:27])
#> letters[24:27] β Freq. Percent Cum.
#> ββββββββββββββββββΌβββββββββββββββββββββββββββββββββββ
#> x β 1 25.0 25.0
#> y β 1 25.0 50.0
#> z β 1 25.0 75.0
#> NA β 1 25.0 100.0
#> ββββββββββββββββββΌβββββββββββββββββββββββββββββββββββ
#> Total β 4 100.0 Β
# ftab() displays only flat contingency tables (here, with two variables)
mtcars %>% ftab(cyl, gear)
#> cyl β gear Freq. Percent Cum.
#> βββββββββββββΌβββββββββββββββββββββββββββββββββββββββββββββββ
#> 4 β 3 1 3.1 3.1
#> 4 β 4 8 25.0 28.1
#> 4 β 5 2 6.2 34.3
#> ------------βΌ-----------------------------------------------
#> 6 β 3 2 6.2 40.5
#> 6 β 4 4 12.5 53.0
#> 6 β 5 1 3.1 56.1
#> ------------βΌ-----------------------------------------------
#> 8 β 3 12 37.5 93.6
#> 8 β 5 2 6.2 99.8
# tab1() displays one-way tables for each variable
mtcars %>% tab1(cyl, gear)
#> cyl β Freq. Percent Cum.
#> βββββββββββββΌβββββββββββββββββββββββββββββββββββ
#> 4 β 11 34.4 34.4
#> 6 β 7 21.9 56.3
#> 8 β 14 43.8 100.0
#> βββββββββββββΌβββββββββββββββββββββββββββββββββββ
#> Total β 32 100.0 Β
#>
#> gear β Freq. Percent Cum.
#> βββββββββββββΌβββββββββββββββββββββββββββββββββββ
#> 3 β 15 46.9 46.9
#> 4 β 12 37.5 84.4
#> 5 β 5 15.6 100.0
#> βββββββββββββΌβββββββββββββββββββββββββββββββββββ
#> Total β 32 100.0 Β
# tab2() displays two-way tables for all variable combinations
mtcars %>% tab2(cyl, gear, am)
#> β gear β
#> cyl β 3 4 5 β Total
#> ββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββ
#> 4 β 1 8 2 β 11
#> 6 β 2 4 1 β 7
#> 8 β 12 0 2 β 14
#> ββββββββββββΌββββββββββββββββββββββββββββββββββΌββββββββββ
#> Total β 15 12 5 β 32
#>
#> β am β
#> cyl β 0 1 β Total
#> ββββββββββββΌβββββββββββββββββββββββΌββββββββββ
#> 4 β 3 8 β 11
#> 6 β 4 3 β 7
#> 8 β 12 2 β 14
#> ββββββββββββΌβββββββββββββββββββββββΌββββββββββ
#> Total β 19 13 β 32
#>
#> β am β
#> gear β 0 1 β Total
#> ββββββββββββΌβββββββββββββββββββββββΌββββββββββ
#> 3 β 15 0 β 15
#> 4 β 4 8 β 12
#> 5 β 0 5 β 5
#> ββββββββββββΌβββββββββββββββββββββββΌββββββββββ
#> Total β 19 13 β 32
# ta() is a shortened alias for tab(), inspired by Stata
mtcars %>% ta(gear)
#> gear β Freq. Percent Cum.
#> βββββββββββββΌβββββββββββββββββββββββββββββββββββ
#> 3 β 15 46.9 46.9
#> 4 β 12 37.5 84.4
#> 5 β 5 15.6 100.0
#> βββββββββββββΌβββββββββββββββββββββββββββββββββββ
#> Total β 32 100.0 Β
Please note that the tidytab project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.