ctable: Create a contingency table from data and description (as a...

View source: R/array_fun.R

ctableR Documentation

Create a contingency table from data and description (as a table).

Description

ctable yields a contingency table (as a table) in analogy to creating an array by array.

Usage

ctable(
  data,
  dim = length(data),
  dimnames = NULL,
  by_row = FALSE,
  as_df = FALSE,
  quiet = FALSE
)

Arguments

data

The data to be used (required; frequency counts, as a vector of positive integers).

dim

The dimensions to be used (as a numeric vector). Default: dim = length(data).

dimnames

The dimension names to be used (as a list). Default: dimnames = NULL, using add_dimnames per default.

by_row

(Boolean): Are data to be read in a by-row fashion? Default: by_row = FALSE.

as_df

(Boolean): Return result as a data.frame? Default: as_df = FALSE.

quiet

Boolean: Hide feedback messages? Default: quiet = FALSE (i.e., show messages).

Details

ctable internally passes its arguments to array before coercing the resulting array into a table or data.frame.

The order of elements in its arguments data, dim, and dimnames must correspond to the defaults of array. For instance, data are entered in a by-column fashion and the dimensions in dimnames from left/y, top/x, to group/table/z, etc.

Value

A table.

See Also

subtable for extracting a subset of a table; sublist for extracting subsets of a list; table and array for the underlying data structures.

Other array functions: add_dimnames(), expand_freq_table(), flatten_array(), subtable()

Examples

# Prepare dimnames (as lists): 
l_24 <- list(rows = c("A", "B"), cols = letters[1:4])
l432 <- list(Ys = letters[1:4], Xs = LETTERS[1:3], group = c("I", "II"))

# Standard use:
ctable(1:8,  dim = c(2,4 ), dimnames = l_24)
ctable(1:24, dim = c(4, 3, 2), dimnames = l432)

# by row:
ctable(1:8,  dim = c(2, 4), dimnames = l_24, by_row = TRUE)
ctable(1:24, dim = c(4, 3, 2), dimnames = l432, by_row = TRUE)

# Return as df:
ctable(1:8, dim = c(2, 4), dimnames = l_24, as_df = TRUE)
ctable(1:24, dim = c(4, 3, 2), dimnames = l432, as_df = TRUE)

# No dimnames:
ctable(1:8, dim = c(2, 4))
ctable(1:8, dim = c(2, 2, 2))

# Non-integers:
ctable(1:9/3, dim = c(3, 3), quiet = FALSE)


hneth/i2ds documentation built on Jan. 25, 2024, 2:22 p.m.