umx (version 1.9.1)

umx_aggregate: 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 summarising 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 = NA, what = c("mean_sd",
  "n"), digits = 2, kable = TRUE)

Arguments

formula

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

data

frame to aggregate

what

function to use. Defaults to reporting "mean (sd)"

digits

for rounding of results

kable

Report as a formatted table? (Default is TRUE)

Value

- table

References

- https://github.com/tbates/umx, https://tbates.github.io

See Also

- umx_apply, aggregate

Other Reporting Functions: loadings.MxModel, tmx_is.identified, umxAPA, umxFactorScores, umxGetParameters, umxReduce, umxSummary, umx_APA_pval, umx_parameters, umx_print, umx_show, umx_time, umx

Examples

Run this code
# NOT RUN {
# =====================================
# = 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 table markdown table
umx_aggregate(mpg ~ cyl, data = mtcars, kable = FALSE)

# ============================================
# = 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)
# }

Run the code above in your browser using DataLab