Function to calculate the mean and deviations from mean
Function to calculate the mean and deviations from mean
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.
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.
Returns
A list of the mean (first element) and deviations from the mean (second element).
Examples
## simple example showing what it doesmeanDeviations(1:10)## example use case, applied to a data.tablelibrary(data.table)d <- as.data.table(iris)d[, c("BSepal.Length","WSepal.Length"):= meanDeviations(Sepal.Length), by = Species]str(d)rm(d)