umx_aggregate: Convenient formula-based cross-tabs & built-in summary...

umx_aggregateR Documentation

Convenient formula-based cross-tabs & built-in summary functions

Description

A common task is preparing summary tables, aggregating over some grouping factor. Like mean and sd of age, by sex. R's aggregate() function is useful and powerful, allowing xtabs based on a formula.

umx_aggregate makes using it a bit easier. In particular, it has some common functions for summarizing data built-in, like "mean (sd)" (the default).

umx_aggregate(mpg ~ cyl, data = mtcars, what = "mean_sd")

cyl mpg
4 (n = 11) 26.66 (4.51)
6 (n = 7) 19.74 (1.45)
8 (n = 14) 15.1 (2.56)

Usage

umx_aggregate(
  formula = DV ~ condition,
  data = df,
  what = c("mean_sd", "n"),
  digits = 2,
  report = c("markdown", "html", "txt")
)

Arguments

formula

The aggregation formula. e.g., DV ~ condition.

data

frame to aggregate (defaults to df for common case)

what

function to use. Default reports "mean (sd)".

digits

to round results to.

report

Format for the table: Default is markdown.

Value

  • table

References

See Also

  • umx_apply(), aggregate()

Other Reporting Functions: umxAPA(), umxFactorScores(), umxGetLatents(), umxGetManifests(), umxGetModel(), umxGetParameters(), umxParameters(), umx_time(), umx

Examples

# =====================================
# = Basic use, compare with aggregate =
# =====================================
aggregate(mpg ~ cyl, FUN = mean, na.rm = TRUE, data = mtcars)
umx_aggregate(mpg ~ cyl, data = mtcars)

# =============================================
# = Use different (or user-defined) functions =
# =============================================
umx_aggregate(mpg ~ cyl, data = mtcars, what = "n")
umx_aggregate(mpg ~ cyl, data = mtcars, what = function(x){sum(!is.na(x))})

# turn off markdown
umx_aggregate(mpg ~ cyl, data = mtcars, report = "txt")

# ============================================
# = More than one item on the left hand side =
# ============================================
umx_aggregate(cbind(mpg, qsec) ~ cyl, data = mtcars, digits = 3)
# Transpose table
t(umx_aggregate(cbind(mpg, qsec) ~ cyl, data = mtcars))

## Not run: 
umx_aggregate(cbind(moodAvg, mood) ~ condition, data = study1)

## End(Not run)

umx documentation built on May 29, 2024, 5:40 a.m.