janitor (version 2.2.0)

tabyl: Generate a frequency table (1-, 2-, or 3-way).


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, ...)


Returns a data.frame with frequencies and percentages of the tabulated variable(s). A 3-way tabulation returns a list of data.frames.



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)


should counts of NA values be displayed? In a one-way tabyl, the presence of NA values triggers an additional column showing valid percentages(calculated excluding NA values).


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.


the column name of the first variable.


(optional) the column name of the second variable (the rows in a 2-way tabulation).


(optional) the column name of the third variable (the list in a 3-way tabulation).


Run this code

tabyl(mtcars, cyl)
tabyl(mtcars, cyl, gear)
tabyl(mtcars, cyl, gear, am)

# or using the %>% pipe
mtcars %>%
  tabyl(cyl, gear)

# illustrating show_na functionality:
my_cars <- rbind(mtcars, rep(NA, 11))
my_cars %>% tabyl(cyl)
my_cars %>% tabyl(cyl, show_na = FALSE)

# Calling on a single vector not in a data.frame:
val <- c("hi", "med", "med", "lo")

Run the code above in your browser using DataCamp Workspace