A fully-featured alternative to table()
. Results are data.frames and can be formatted and enhanced with janitor's family of adorn_
functions.
Specify a data.frame and the one, two, or three unquoted column names you want to tabulate. Three variables generates a list of 2-way tabyls, split by the third variable.
Alternatively, you can tabulate a single variable that isn't in a data.frame by calling tabyl
on a vector, e.g., tabyl(mtcars$gear)
.
tabyl(dat, ...) # S3 method for default tabyl(dat, show_na = TRUE, show_missing_levels = TRUE, ...) # S3 method for data.frame tabyl(dat, var1, var2, var3, show_na = TRUE, show_missing_levels = TRUE, ...)
dat | a data.frame containing the variables you wish to count. Or, a vector you want to tabulate. |
---|---|
... | the arguments to tabyl (here just for the sake of documentation compliance, as all arguments are listed with the vector- and data.frame-specific methods) |
show_na | should counts of |
show_missing_levels | should counts of missing levels of factors be displayed? These will be rows and/or columns of zeroes. Useful for keeping consistent output dimensions even when certain factor levels may not be present in the data. |
var1 | the column name of the first variable. |
var2 | (optional) the column name of the second variable (the rows in a 2-way tabulation). |
var3 | (optional) the column name of the third variable (the list in a 3-way tabulation). |
Returns a data.frame with frequencies and percentages of the tabulated variable(s). A 3-way tabulation returns a list of data.frames.
tabyl(mtcars, cyl)#> cyl n percent #> 4 11 0.34375 #> 6 7 0.21875 #> 8 14 0.43750tabyl(mtcars, cyl, gear)#> cyl 3 4 5 #> 4 1 8 2 #> 6 2 4 1 #> 8 12 0 2tabyl(mtcars, cyl, gear, am)#> $`0` #> cyl 3 4 5 #> 4 1 2 0 #> 6 2 2 0 #> 8 12 0 0 #> #> $`1` #> cyl 3 4 5 #> 4 0 6 2 #> 6 0 2 1 #> 8 0 0 2 #># or using the %>% pipe mtcars %>% tabyl(cyl, gear)#> cyl 3 4 5 #> 4 1 8 2 #> 6 2 4 1 #> 8 12 0 2#> cyl n percent valid_percent #> 4 11 0.33333333 0.34375 #> 6 7 0.21212121 0.21875 #> 8 14 0.42424242 0.43750 #> NA 1 0.03030303 NAmy_cars %>% tabyl(cyl, show_na = FALSE)#> cyl n percent #> 4 11 0.34375 #> 6 7 0.21875 #> 8 14 0.43750#> val n percent #> hi 1 0.25 #> lo 1 0.25 #> med 2 0.50