crtabs {crunch}R Documentation

Crunch xtabs: Crosstab and otherwise aggregate variables in a Crunch Dataset

Description

Create a contingency table or other aggregation from cross-classifying variables in a CrunchDataset, expanding on the notation allowed in stats::xtabs() to tailor to the kinds of calculations available in crunch.

Usage

crtabs(
  formula,
  data,
  weight = crunch::weight(data),
  useNA = c("no", "ifany", "always")
)

Arguments

formula

a stats::formula object that specifies that query to calculate. See Details for more information.

data

an object of class CrunchDataset

weight

a CrunchVariable that has been designated as a potential weight variable for data, or NULL for unweighted results. Default is the currently applied weight().

useNA

whether to include missing values in tabular results. See base::table().

Details

There are 3 types of queries supported:

Value

an object of class CrunchCube

See Also

weight()

Examples

## Not run: 
# Crosstab of people by `age_cat`:
crtabs(~age_cat, ds)

# Aggregation of means of income by `age_cat`
crtabs(mean(income) ~ age_cat, ds)

# Scorecard of multiple MRs with aligned subvariables
crtabs(~scorecard(trust_mr, value_mr, quality_mr), ds)
# Can also pre-define the variables in a scorecard with
mr_list <- list(ds$trust_mr, ds$value_mr, ds$quality_mr)
crtabs(~scorecard(vars = mr_list), ds)

# Crosstab of people by `age_cat` and the reasons for enjoying a brand (cat array)
crtabs(~age_cat + enjoy_array, ds)

# Crosstab of people by `age_cat` and the `enjoy_array` (cat array)
# But manually choosing the order of the dimensions
crtabs(~subvariables(enjoy_array) + age_cat + categories(enjoy_array), ds)

# Aggregation of means & standard deviations of income by `age_cat`
crtabs(list(mean = mean(income), sd = sd(income)) ~ age_cat, ds)

## End(Not run)

[Package crunch version 1.30.4 Index]