factor2var: Create Dummy Variables

View source: R/factor2var.R

factor2varR Documentation

Create Dummy Variables

Description

Create dummy variables from a categorical variable.

Usage

factor2var(
  x_value,
  x_contrasts = "contr.treatment",
  prefix = "",
  add_rownames = TRUE
)

Arguments

x_value

The vector of categorical variable.

x_contrasts

The contrast to be used. Default is "constr.treatment".

prefix

The prefix to be added to the variables to be created. Default is "".

add_rownames

Whether row names will be added to the output. Default is TRUE.

Details

Its main use is for creating dummy variables (indicator variables) from a categorical variable, to be used in lavaan::sem().

Optionally, the other contrasts can be used through the argument x_contrasts.

Value

It always returns a matrix with the number of rows equal to the length of the vector (x_value). If the categorical has only two categories and so only one dummy variable is needed, the output is still a one-column "matrix" in R.

Examples

dat <- data_mod_cat
dat <- data.frame(dat,
                  factor2var(dat$w, prefix = "gp", add_rownames = FALSE))
head(dat[, c("w", "gpgroup2", "gpgroup3")], 15)


manymome documentation built on Oct. 4, 2024, 5:10 p.m.