center.by.group: Center Numeric Variable Within Each of Several Cells

View source: R/center.by.group.R

center.by.groupR Documentation

Center Numeric Variable Within Each of Several Cells

Description

Centers the elements of a numeric vector around the means of subsets defined by one or more grouping variables. For instance, center Likert scale ratings within each participant, or center student achievement scores around the mean within a particular school rather than the overall mean.

Usage

center.by.group(x, INDEX)

Arguments

x

a numeric vector

INDEX

list of one or more grouping variables, typically factors, each of the same length as x

Value

numeric vector with x centered separately within each level of INDEX

See Also

zscore.by.group to center within groups and scale by the standard deviation of each group.

Examples

my.data <- data.frame(Subject=c(rep('S1',6), rep('S2',6)),
                      Session=c(rep('Day1',3), rep('Day2',3),
                                rep('Day1',3), rep('Day2',3)),
                      Rating=c(10,30,20,20,40,30,15,35,25,25,45,35))
# center within each subject:
my.data$Rating.c <- center.by.group(my.data$Rating, my.data$Subject)
# center within each subject x session pairing:
my.data$Rating.c <- center.by.group(my.data$Rating, list(my.data$Subject,
my.data$Session))

sfraundorf/psycholing documentation built on April 23, 2022, 2:50 a.m.