Package 'tidytab'

Title: Create Tables of Frequencies
Description: Functions to create tables of frequencies.
Authors: Gustavo Velásquez <[email protected]>
Maintainer: Gustavo Velásquez <[email protected]>
License: MIT + file LICENSE
Version: 0.0.0.9000
Built: 2024-12-16 06:01:20 UTC
Source: https://github.com/gvelasq/tidytab

Help Index


A reimagination of %in% for partial string matching

Description

%gin% is a reimagination of %in% using grepl() for partial string matching.

Usage

pattern %gin% x

Arguments

pattern

Character string to be matched.

x

R object to be matched against. The object must be a character vector or an object coercible by as.character() to a character vector.

References

%gin% was first written for @ivelasq's r-data-recipes GitHub repository.

Examples

# %in% evaluates to FALSE because it looks for full string matches
"t" %in% "tonic"

# %gin% evaluates to TRUE
"t" %gin% "tonic"

# %gin% can be used with tab()
tab("Toyota" %gin% rownames(mtcars))

Browse data

Description

br() is an alias for utils::View() and invokes the data viewer. See utils::View() for details. br() invisibly returns its input so that it can be dropped into magrittr pipe chains.

Usage

br(x, title)

Arguments

x

An R object coercible into a data frame.

title

Optional title for viewer window.


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)