Learn R Programming

JWileymisc (version 0.3.1)

meanDeviations: Function to calculate the mean and deviations from mean

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

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

Run this code
# NOT RUN {
## simple example showing what it does
meanDeviations(1:10)

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

Run the code above in your browser using DataLab