indicator: Create indicator matrix from character vector.

Description Usage Arguments Value Examples

View source: R/indicator.R

Description

Rows of an indicator matrix correspond to "observations". Columns correspond to "conditions" (e.g., levels of a factor). Each observation "belongs" to only one condition. This membership is indicated by a 1 in the corresponding column (otherwise 0).

Usage

1

Arguments

x

character vector of length n

Value

Indicator matrix. Column names taken from unique(x).

Examples

1
2
3
4
5
x <- rep(letters[1:4], each = 4)
M <- indicator(x)
nrow(M) == length(x)
ncol(M) == length(unique(x))
M == stats::model.matrix(~ as.factor(x) + 0)

mcfreund/mfutils documentation built on Feb. 6, 2022, 4:57 a.m.