Learn R Programming

janitor (version 0.2.1)

tabyl: Generate a frequency table from a vector.

Description

Create a frequency table of a variable, returned as a data.frame. It shows counts, percentages and, if NA values are present, valid percentages (calculated excluding NA values). A fully-featured alternative to table().

tabyl can be called in two ways:

1) It can simply be called on a vector, like tabyl(mtcars$gear).

2) A data.frame can be provided as the first argument, followed by an unquoted column name to tabulate. This enables passing in a data.frame from a %>% pipeline, like mtcars %>% tabyl(gear).

Usage

tabyl(...)
"tabyl"(vec, sort = FALSE, show_na = TRUE, ...)
"tabyl"(.data, ...)

Arguments

...
additional arguments, if calling tabyl on a data.frame.
vec
the vector to tabulate. If supplying a data.frame, this should be an unquoted column name.
sort
a logical value indicating whether the resulting table should be sorted in descending order of n.
show_na
a logical value indicating whether the count of NA values should be displayed, along with an additional column showing valid percentages.
.data
(optional) a data.frame, in which case vec should be an unquoted column name.

Value

Returns a data.frame with the frequencies and percentages of the tabulated variable.

Examples

Run this code
# Calling on a vector:
val <- c("hi", "med", "med", "lo")
tabyl(val)
tabyl(mtcars$cyl, sort = TRUE)

# Passing in a data.frame using a pipeline:
library(dplyr) # to access the pipe operator
mtcars %>% tabyl(cyl, sort = TRUE)

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

Run the code above in your browser using DataLab