meanDeviations: Function to calculate the mean and deviations from mean

meanDeviationsR Documentation

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

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

JWiley/multilevelTools documentation built on April 1, 2024, 9:56 p.m.