tab: Create tables of frequencies

View source: R/tab.R

tabR Documentation

Create tables of frequencies

Description

tab() creates n-way tables of frequencies in the R console, similar to those created by Stata's tabulate function. When three or more variables are passed to tab(), only flat tables are displayed. ta() is a shortened alias for tab().

ftab() creates only flat tables of frequencies.

The convenience functions tab1() and tab2() are inspired by functions of the same name in Stata. They allow rapid tabulation of a set of variables. tab1() creates one-way tables of frequencies for each listed variable. tab2() creates two-way tables of frequencies for all listed variable combinations.

Usage

tab(x, ..., m = TRUE)

ta(x, ..., m = TRUE)

ftab(x, ..., m = TRUE)

tab1(x, ..., m = TRUE)

tab2(x, ..., m = TRUE)

Arguments

x

A vector, data.frame, or tibble.

...

A comma separated list of unquoted variable names or positions. Select helpers from dplyr and tidyselect are supported.

m

If TRUE (the default), missing values are reported.

Details

If a single variable is passed to tab(), a table of frequencies is printed (with a total row and columns 'Freq.', 'Percent', and 'Cum.').

If two variables are passed to tab(), a special 2x2 contingency table is printed (with a total row and a total column).

If three or more variables are passed to tab(), a flat contingency table is printed (with columns 'Freq.', 'Percent', and 'Cum.').

The invisibly returned tibble excludes total rows and columns to avoid collision of variable classes.

Value

A tibble containing a table of frequencies for the variables listed in ...

See Also

The statar package by Matthieu Gomez provides a tab() function with output similar to tidytab's ftab(). Both packages use a variant of statascii() to format tables for display in the R console. Differences between the packages include:

  • tidytab supports select helpers from dplyr and tidyselect.

  • tidytab displays tables in colors: dark grey for block drawing characters and red for NAs.

  • tidytab allows for tabulation of named and unnamed vectors.

  • tidytab implements automatic table wrapping for tables wider than the R console.

  • tidytab's tab() and ftab() display a total row with total frequencies for one-way tabulations.

  • tidytab's tab() displays a special 2x2 contingency table for two-way tabulations (flat two-way tables are available with ftab()).

  • tidytab's convenience functions tab1() and tab2() allow for rapid tabulation of a set of variables into either one- or two-way tables.

The janitor package by Sam Firke provides the tabyl() function for SPSS-like tables of frequencies and adornments.

Base R provides the ftable() and xtabs() functions for unadorned tables of frequencies.

Examples

# one-way table of frequencies
mtcars |> tab(cyl)

# two-way table of frequencies (a special 2x2 contingency table)
mtcars |> tab(cyl, gear)

# flat contingency tables of three (or more) variables
mtcars |> tab(cyl, gear, am)

# tables wider than the R console are automatically wrapped
mtcars |> tab(cyl, gear, am, vs)

# missing values are displayed in red
tab(letters[24:27])

# ftab() displays only flat contingency tables (here, with two variables)
mtcars |> ftab(cyl, gear)

# tab1() displays one-way tables for each variable
mtcars |> tab1(cyl, gear)

# tab2() displays two-way tables for all variable combinations
mtcars |> tab2(cyl, gear, am)

# ta() is a shortened alias for tab(), inspired by Stata
mtcars |> ta(gear)


gvelasq/tidytab documentation built on April 21, 2024, 4:47 p.m.