Learn R Programming

statuser (version 0.1.9)

table2: Enhanced alternative to table()

Description

The function table does not show variable names when tabulating from a dataframe, requires running another function, prop.table, to tabulate proportions and yet another function, chisq.test to test difference of proportions. table2 does what those three functions do, producing easier to read output, and always shows variable names.

Value

A list (object of class "table2") with the following components:

  • freq: frequency table

  • prop: proportions table

  • chisq: chi-square test

Arguments

...

same arguments as table, plus the arguments shown below

prop

report a table with:

  • prop="all": Proportions for full table (each cell / total)

  • prop="row": Proportions by row ('rows' also accepted)

  • prop="col": Proportions by columns ('cols', 'column', 'columns' also accepted)

digits

Number of decimal values to show for proportions

chi

Logical. If TRUE, performs a chi-square test on frequency table, reports results in APA format

correct

Logical. If TRUE, applies Yates' continuity correction for 2x2 tables in the chi-square test. Default is FALSE (no correction).

Examples

Run this code
# Create example data
df <- data.frame(
  group = c("A", "A", "B", "B", "A"),
  status = c("X", "Y", "X", "Y", "X")
)

# Enhanced table with variable names (2 variables)
table2(df$group, df$status)

# Enhanced table with variable names (3 variables)
df3 <- data.frame(
  x = c("A", "A", "B", "B"),
  y = c("X", "Y", "X", "Y"),
  z = c("high", "low", "high", "low")
)
table2(df3$x, df3$y, df3$z)

# Table with proportions
table2(df$group, df$status, prop = 'all')  # Overall proportions
table2(df$group, df$status, prop = 'row')  # Row proportions
table2(df$group, df$status, prop = 'col')  # Column proportions

# Table with chi-square test
table2(df$group, df$status, chi = TRUE,prop='all')

Run the code above in your browser using DataLab