group_demean: Subtract mean values by grouping factor

Description Usage Arguments Value Examples

View source: R/demean.R

Description

Subtract mean values by grouping factor. Accepts either two vectors (data and groups) or a data.frame and a formula of the form data ~ groups.

Usage

1
2
3
4
5
6
7
8
9
group_demean(...)

## S3 method for class 'numeric'
group_demean(x, groups, digits = getOption("digits"),
  named = FALSE, ...)

## S3 method for class 'formula'
group_demean(f, data, digits = getOption("digits"),
  named = FALSE, ...)

Arguments

...

arguments passed to specific methods.

x

a numeric vector.

groups

a vector of the same length as x that contains a group indicator corresponding to each element of x.

digits

an integer passed directly to round.

named

If TRUE, the names of the ouput vector are set to the original values of the numeric input data.

f

a formula. For use with the data.frame data. Syntax is x ~ groups where x is a numeric column of data that is grouped by groups.

data

a data.frame.

Value

A vector of the same length as the input data (either x or the input column from the data.frame data) where group means have been subtracted from each element in the input data. Specifically, residuals are reported the ordinary least squares regression lm(x ~ factor(groups)).

Examples

1
2
3
4
5
6
# using a numeric vector and corresponding grouping vector
x <- runif(50)
g <- rep(letters[1:5], 10)
group_demean(x, g)
# using a formula and data.frame
group_demean(mpg ~ cyl, mtcars)

sboysel/boysel documentation built on May 29, 2019, 3:24 p.m.