as.prop.table: Convert a crosstable to a frequency or a propensity table

View source: R/data_tabulate.R

as.prop.tableR Documentation

Convert a crosstable to a frequency or a propensity table

Description

as.prop.table() is an S3 generic. It can be used on objects of class datawizard_crosstab created by data_tabulate() when it was run with the arguments by and proportions.

Usage

as.prop.table(x, ...)

## S3 method for class 'datawizard_crosstab'
as.prop.table(x, remove_na = TRUE, simplify = FALSE, verbose = TRUE, ...)

## S3 method for class 'datawizard_tables'
as.data.frame(
  x,
  row.names = NULL,
  optional = FALSE,
  ...,
  stringsAsFactors = FALSE,
  add_total = FALSE
)

## S3 method for class 'datawizard_table'
as.table(x, remove_na = TRUE, simplify = FALSE, verbose = TRUE, ...)

Arguments

x

An object created by data_tabulate(). It must be of class datawizard_crosstab for as.prop.table().

...

not used.

remove_na

Logical, if FALSE, missing values are included in the frequency or crosstable, else missing values are omitted. Note that the default for the as.table() method is remove_na = TRUE, so that missing values are not included in the returned table, which makes more sense for post-processing of the table, e.g. using chisq.test().

simplify

Logical, if TRUE, the returned table is simplified to a single table object if there is only one frequency or contingency table input. Else, always for multiple table inputs or when simplify = FALSE, a list of tables is returned. This is only relevant for the as.table() methods. To ensure consistent output, the default is FALSE.

verbose

Toggle warnings and messages.

row.names

NULL or a character vector giving the row names for the data frame. Missing values are not allowed.

optional

logical. If TRUE, setting row names and converting column names (to syntactic names: see make.names) is optional. Note that all of R's base package as.data.frame() methods use optional only for column names treatment, basically with the meaning of data.frame(*, check.names = !optional). See also the make.names argument of the matrix method.

stringsAsFactors

logical: should the character vector be converted to a factor?

add_total

For crosstables (i.e. when by is not NULL), a row and column with the total N values are added to the data frame. add_total has no effect in as.data.frame() for simple frequency tables.

See Also

data_tabulate

Examples

data(efc)

# Some cross tabulation
cross <- data_tabulate(efc, select = "e42dep", by = "c172code", proportions = "row")
cross

# Convert to a propensity table
as.prop.table(cross)

# Convert to data.frame
result <- data_tabulate(efc, "c172code", by = "e16sex")
as.data.frame(result)
as.data.frame(result)$table
as.data.frame(result, add_total = TRUE)$table

# Convert to a table that can be passed to chisq.test()

out <- data_tabulate(efc, "c172code", by = "e16sex")
# we need to simplify the output, else we get a list of tables
tbl <- as.table(out, simplify = TRUE)
tbl
suppressWarnings(chisq.test(tbl))

# apply chisq.test to each table
out <- data_tabulate(efc, c("c172code", "e16sex"))
suppressWarnings(lapply(as.table(out), chisq.test))

# can also handle grouped data frames
d <- data_group(mtcars, "am")
x <- data_tabulate(d, "cyl", by = "gear")
as.table(x)

datawizard documentation built on April 26, 2026, 5:06 p.m.