tab: Table of frequencies or relative frequencies bordered with...

View source: R/tab.R

tabR Documentation

Table of frequencies or relative frequencies bordered with totals and including NAs

Description

Generates a table of frequencies or relative frequencies or relative percentages

Usage

tab(x, ...)

## S3 method for class 'table'
tab(x, ...)

## S3 method for class 'matrix'
tab(x, ...)

## S3 method for class 'array'
tab(x, ...)

## S3 method for class 'formula'
tab(fmla, data = sys.frame(sys.parent()), ...)

## S3 method for class 'data.frame'
tab(
  x,
  fmla,
  total.margins = TRUE,
  useNA = "ifany",
  pct = NULL,
  pr = NULL,
  test = FALSE,
  weights = NULL,
  na.rm = NULL,
  all.label = "All",
  simulate = FALSE,
  B = 2000,
  ...
)

## Default S3 method:
tab(
  ...,
  total.margins = TRUE,
  pct = NULL,
  pr = NULL,
  useNA = "ifany",
  test = simulate,
  weights = NULL,
  na.rm = NULL,
  all.label = "All",
  simulate = FALSE,
  B = 2000
)

Arguments

...

as with table, one or more objects which can be interpreted as factors (including character strings), or a list (or data frame) whose components can be so interpreted.

fmla

a formula whose right-hand side names the variables to be used for tabulation. The optional left-hand side specifies a variable to be used for weights.

data

a data frame in which formula are interpreted

total.margins

if FALSE, generate table without margins

useNA

whether to include NA levels. The default is "ifany". Can also be set to "no" or "always".

pct

margins to be scaled to sum to 100. This is the vector of margin indices on which percentages are conditioned. For example, with the call tab(~ A + B + C, data, pct = 2:3), the table will contain conditional percentages for variable A conditional of combinations of B and C.

pr

margins to be scaled to sum to 1. This is the vector of margin indices on which percentages are conditioned.

test

(default FALSE) use chisq.test

weights

(not working temporarily) instead of generating a frequency table, generate a table with the sum of the weights

simulate

(default FALSE) simulate p-value with chisq.test

B

(default 2000) number of replications for simulation

keep

names of margins to keep with 'Tab', default = "All". To drop all margins, use default = "".

Value

An object of class 'table' of dimension equal to the number of variables, with optional margins showing totals. Elements of the matrix can be frequencies, relative frequencies, percentages or sums of weights.

Methods (by class)

  • tab(table): method class table

  • tab(matrix): method for matrices

  • tab(array): method for arrays

  • tab(formula): method for formulas

  • tab(data.frame): method for data frames

  • tab(default): default method

Author(s)

Georges Monette

See Also

tab_ to drop "Total" margins and tab__ to drop "Total" and "All" margins.

Examples

titanic <- as.data.frame(Titanic)
head(titanic)
tab(titanic, Freq ~ Sex + Survived + Age , test = T)
tab(titanic, Freq ~ Sex + Age)
tab(titanic, Freq ~ Sex + Survived + Age)
round(tab(titanic, Freq ~ Sex + Survived + Age,
    pct = c(1,3)),2)
round(Tab(titanic, Freq ~ Sex + Survived + Age,
    pct = c(1,3)),2)
round(Tab(titanic, Freq ~ Sex + Survived + Age,
    pct = c(1,3), keep = ""),2)

gmonette/spida2 documentation built on July 14, 2024, 12:45 p.m.