grouped_stats: Grouped Weighted Means, Quartiles, or Variances

View source: R/grouped_stats.R

grouped_statsR Documentation

Grouped Weighted Means, Quartiles, or Variances

Description

Calculates weighted means, quartiles, or variances (and counts) of a variable grouped by optional columns. By default, counts are not weighted, even if there is a weighting variable.

Usage

grouped_stats(
  data,
  x,
  w = NULL,
  by = NULL,
  stats = c("mean", "quartiles", "variance"),
  counts = TRUE,
  counts_weighted = FALSE,
  counts_name = "counts",
  value_name = x,
  q1_name = "q1",
  q3_name = "q3",
  ...
)

Arguments

data

A data.frame.

x

Variable name in data to summarize.

w

Optional name of the column in data with case weights.

by

An optional vector of column names in data used to group the results.

stats

Statistic to calculate: "mean", "quartiles", or "variance".

counts

Should group counts be added?

counts_weighted

Should counts be weighted by the case weights? If TRUE, the sum of w is returned by group.

counts_name

Name of column in the resulting data.frame containing the counts.

value_name

Name of the resulting column with mean, median, or variance.

q1_name

Name of the resulting column with first quartile values. Only relevant if stats = "quartiles".

q3_name

Name of the resulting column with third quartile values. Only relevant if stats = "quartiles".

...

Additional arguments passed to corresponding ⁠weighted_*()⁠ functions in MetricsWeighted.

Value

A data.frame with columns by, x, and optionally counts_name.

Examples

grouped_stats(iris, "Sepal.Width")
grouped_stats(iris, "Sepal.Width", stats = "quartiles")
grouped_stats(iris, "Sepal.Width", stats = "variance")
grouped_stats(iris, "Sepal.Width", w = "Petal.Width", counts_weighted = TRUE)
grouped_stats(iris, "Sepal.Width", by = "Species")

flashlight documentation built on May 31, 2023, 6:19 p.m.