Learn R Programming

doBy (version 4.5-15)

scaleBy: Groupwise scaling and centering of numeric columns in a dataframe

Description

Groupwise scaling and centering of numeric columns in a dataframe. Obtained by first splitting a dataframe and then calling scale on each stratum.

Usage

scaleBy(formula, data, center = TRUE, scale = TRUE, details=0)

Arguments

formula
Either a two-sided formula or a list. A dot (.) is allowed on both left and right hand side of formula. See 'details' for the meaning of this.
data
A dataframe
center
If TRUE then data is centered to have mean zero
scale
If TRUE then data is scaled to have variance one
details
If larger than zero then information about grouping etc. is printed.

Value

A dataframe with the same columns as the input dataframe, but the scaled / centered values are put into the relevant columns.

Details

A typical formula is y1+y2~f1+f2 where y1 and y2 are numeric variables and f1 and f2 can be of any type. For each cross-combination of the values of f1 and f2, the variables (y1,y2) are centered/scaled. It is valid to write .~f1+f2. In this case the variables to be centered/scaled are taken to be all numeric variables in the dataframe except those that a listed on the right hand side of the formula.

It is valid to write y1+y2~.. In this case the stratification is taken to be by all non-numeric variables. If there are no non-numeric variables, then no stratification is made and a 'global' centering/scaling is made.

It is valid to write .~.. In this case the variables to be centered/scaled are taken to be all numeric variables in the dataframe. The stratification is made by all non-numeric variables. If there are no non-numeric variables, then no stratification is made and a 'global' centering/scaling is made.

See Also

lapplyBy, orderBy, splitBy, summaryBy, transformBy,

Examples

Run this code

## The following forms are equivalent:
scaleBy(conc+rate ~ state, data=Puromycin)
scaleBy(list(c("conc","rate"), "state"), data=Puromycin)
scaleBy(list(c("."), "."), data=Puromycin)
scaleBy(.~., data=Puromycin)

## The same results can be obtained from
lapply(splitBy(~state, data=Puromycin),
	function(.dd) scale(.dd[,sapply(Puromycin,class)=="numeric"]))


## The pig growth data 'dietox'
data(dietox)

# "Remove the effect of time" by centering data within each time point.
dietox2 <- scaleBy(Weight~Time, data=dietox, scale=FALSE)

## Not run: 
# library(lattice)
# xyplot(Weight~Time|Evit+Cu, groups=Pig, data=dietox)
# xyplot(Weight~Time|Evit+Cu, groups=Pig, data=dietox2)
# ## End(Not run)

Run the code above in your browser using DataLab