Learn R Programming

lsr (version 1.0.0)

expandFactors: Expand factors to a set of contrasts

Description

Replaces each factor variable in a data frame with its contrast-coded columns, leaving numeric variables unchanged.

Usage

expandFactors(data, ...)

Value

A data frame with factor columns replaced by numeric contrast columns.

Arguments

data

A data frame.

...

Additional arguments passed to model.matrix, such as contrasts.arg for specifying non-default contrast schemes.

Details

Each factor in data is replaced by the numeric contrast columns that model.matrix would generate for that factor (using treatment contrasts by default). Numeric variables pass through unchanged. This can be helpful when illustrating the connection between ANOVA and regression.

See Also

Examples

Run this code
grading <- data.frame(
  teacher = factor(c("Amy", "Amy", "Ben", "Ben", "Cat")),
  gender  = factor(c("male", "female", "female", "male", "male")),
  grade   = c(75, 80, 45, 50, 65)
)

# expand using the default contrasts (treatment contrasts)
expandFactors(grading)

# specify different contrasts via contrasts.arg
my.contrasts <- list(teacher = "contr.helmert", gender = "contr.treatment")
expandFactors(grading, contrasts.arg = my.contrasts)

Run the code above in your browser using DataLab