Learn R Programming

flashlight (version 0.9.0)

grouped_center: 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, ...)

Value

A numeric vector with centered values in column x.

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).

Examples

Run this code
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)

Run the code above in your browser using DataLab