grouped_center: Grouped, weighted mean centering

View source: R/grouped_center.R

grouped_centerR Documentation

Grouped, weighted mean centering

Description

Centers a numeric variable within optional groups and optional weights. The order of values is unchanged.

Usage

grouped_center(data, x, w = NULL, by = NULL, ...)

Arguments

data

A data.frame.

x

Variable name in data to center.

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.

...

Additional arguments passed to mean calculation (e.g. na.rm = TRUE).

Value

A numeric vector with centered values in column x.

Examples

ir <- data.frame(iris, w = 1)
mean(grouped_center(ir, "Sepal.Width"))
rowsum(grouped_center(ir, "Sepal.Width", by = "Species"), ir$Species)
mean(grouped_center(ir, "Sepal.Width", w = "w"))
rowsum(grouped_center(ir, "Sepal.Width", by = "Species", w = "w"), ir$Species)

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