colDevs | R Documentation |
colDevs
calculates the column deviations of data values from a
central value (mean, median, etc.), possibly stratified by a grouping
variable.
colDevs(x, group, center = mean, group.var = FALSE, ...)
x |
A numeric data frame or matrix. |
group |
A factor (or variable that can be coerced to a factor)
indicating the membership of each observation in |
center |
A function used to center the values (for each group if
|
group.var |
logical. If |
... |
Arguments passed down |
Conceptually, the function is similar to a column-wise
sweep
, by group, allowing an arbitrary center
function.
Non-numeric columns of x
are removed, with a warning.
By default, it returns a numeric matrix containing the deviations from the centering
function. If levels==TRUE
, it returns a data.frame containing the group factor prepended to the
matrix of deviations.
Michael Friendly
colMeans
for column means,
sweep
data(iris)
Species <- iris$Species
irisdev <- colDevs(iris[,1:4], Species, mean)
irisdev <- colDevs(iris[,1:4], Species, median)
# trimmed mean, using an anonymous function
irisdev <- colDevs(iris[,1:4], Species, function(x) mean(x, trim=0.25))
# include the group factor in output
irisdev <- colDevs(iris[,1:4], Species, group.var = "Species")
head(irisdev)
# no grouping variable: deviations from column grand means
# include all variables (but suppress warning for this doc)
irisdev <- suppressWarnings( colDevs(iris) )
# two-way design
colDevs(Plastic[,1:3], Plastic[,"rate"])
colDevs(Plastic[,1:3], Plastic[,"additive"])
# cell deviations
#' colDevs(Plastic[,1:3], interaction(Plastic[,c("rate", "additive")]))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.