trim.by.group: Trim Values Based on Group Means and Standard Deviations

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

trim.by.groupR Documentation

Trim Values Based on Group Means and Standard Deviations

Description

Identifies values of x that are a certain number of standard deviations from the mean within a particular set of grouping variables and treats them as missing (NA) values so that they can be easily deleted (e.g., delete values more than 3 standard deviations from the). For example, trim response times that are more than 2.5 standard deviations from the mean within each cell of a PrimeType x WordFrequency factorial experimental design.

Usage

trim.by.group(x, INDEX, sds = 3, print = TRUE, na.rm = FALSE)

Arguments

x

a numeric vector.

INDEX

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

sds

number of standard deviations away from the mean at which values should be trimmed.

print

a logical value indicating whether to display the number and percentage of trimmed elements.

na.rm

a logical value indicating whether existing NA values should be ignored when computing the mean and standard deviation.

Value

vector in which values more than the specified number of standard deviations from each cell mean have been replaced with NA.

See Also

flag.by.group to identify such observations without changing them.

fence.by.group to replace such observations with the boundary value.

trim to trim values a certain number of standard deviations away from the overall mean rather than a group mean.

Examples

data(sleepstudy, package='lme4')
sleepstudy$Reaction.Trimmed <- trim.by.group(sleepstudy$Reaction,
  sleepstudy$Subject, sds=3) # trim RTs 3 std devs from subject mean

data(VerbAgg, package='lme4')
VerbAgg$Anger.Trimmed <- trim.by.group(VerbAgg$Anger,
  list(VerbAgg$btype, VerbAgg$situ), sds=3)
# trim 3 std devs from behavior type x situation type cell mean

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