centers_by: Centering and/or Standardizing Numeric Data by Group

Description Usage Arguments Details Value See Also Examples

View source: R/quest_functions.R

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

1
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 data specifying the variables.

grp.nm

character vector of colnames from data specifying the groups.

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 center and scale arguments: 1)if center = TRUE and scale = FALSE, then suffix = "_cw", 2) if center = FALSE and scale = TRUE, then suffix = "_sw", 3) if center = TRUE and scale = TRUE, then suffix = "_zw", 4) if center = FALSE and scale = FALSE, then suffix = "".

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

1
2
3
4
5
6
7
8
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

quest documentation built on Sept. 10, 2021, 5:07 p.m.

Related to centers_by in quest...