centers_by {quest} | R Documentation |
Centering and/or Standardizing Numeric Data by Group
Description
centers_by
centers and/or standardized data by group. This is sometimes
called group-mean centering and/or group-SD standardizing. The groups can be
specified by multiple columns in data
(e.g., grp.nm
with length
> 1), and interaction
will be implicitly called to create the groups.
Usage
centers_by(data, vrb.nm, grp.nm, center = TRUE, scale = FALSE, suffix)
Arguments
data |
data.frame of data. |
vrb.nm |
character vector of colnames from |
grp.nm |
character vector of colnames from |
center |
logical vector with length 1 specifying whether group-mean centering should be done. |
scale |
logical vector with length 1 specifying whether group-SD scaling should be done. |
suffix |
character vector with a single element specifying the string to
append to the end of the colnames of the return object. The default depends
on the |
Details
centers_by
first coerces data[vrb.nm]
to a matrix in preparation
for the core of the function, which is essentially lapply(X = split(x =
data[vrb.nm], f = data[grp.nm]), FUN = scale.default)
If the coercion
results in a non-numeric matrix (e.g., any columns in data[vrb.nm]
are
character vectors or factors), then an error is returned.
Value
data.frame of centered and/or standardized variables by group with
colnames specified by paste0(vrb.nm, suffix)
.
See Also
center_by
centers
center
scale.default
Examples
ChickWeight2 <- as.data.frame(ChickWeight) # because the "groupedData" class calls
# `[.groupedData`, which is different than `[.data.frame`
row.names(ChickWeight2) <- as.numeric(row.names(ChickWeight)) / 1000
centers_by(data = ChickWeight2, vrb.nm = c("weight","Time"), grp.nm = "Chick")
centers_by(data = ChickWeight2, vrb.nm = c("weight","Time"), grp.nm = "Chick",
scale = TRUE, suffix = "_within")
centers_by(data = as.data.frame(CO2), vrb.nm = c("conc","uptake"),
grp.nm = c("Type","Treatment"), scale = TRUE) # multiple grouping columns