meanDeviations: Function to calculate the mean and deviations from mean

Description Usage Arguments Value Examples

Description

Tiny helper function to calculate the mean and deviations from the mean, both returned as a list. Works nicely with data.table to calculate a between and within variable.

Usage

1
meanDeviations(x, na.rm = TRUE)

Arguments

x

A vector, appropriate for the mean function.

na.rm

A logical, whether to remove missing or not. Defaults to TRUE.

Value

A list of the mean (first element) and deviations from the mean (second element).

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## simple example showing what it does
meanDeviations(1:10)

## example use case, applied to a data.table
library(data.table)
d <- as.data.table(iris)
d[, c("BSepal.Length", "WSepal.Length") := meanDeviations(Sepal.Length),
  by = Species]
str(d)

rm(d)

multilevelTools documentation built on March 13, 2020, 2:07 a.m.