to.indicators: Construct Indicator Matrix

Description Usage Arguments Value Examples

View source: R/ncpen_util.R

Description

to.indicators converts a categorical variable into a data.frame with indicator (0 or 1) variables for each category.

Usage

1
to.indicators(vec, exclude.base = TRUE, base = NULL, prefix = NULL)

Arguments

vec

a categorical vector.

exclude.base

FALSE means to include all the categories. TRUE means to exclude one category as a base case. If base is not specified, a random category will be removed.

base

a base category removed from the indicator matrix. This option works only when the type variable is set to "exclude.base".

prefix

a prefix to be used for column names of the output matrix. Default is "cat_" if prefix is NULL. For example, if a category vector has values of c("aa", "bb", "cc"), column names of the output matrix will be "cat_aa", "cat_bb" and "cat_cc". If vec is a data.frame and prefix is NULL, then the vec's column name followed by "_" will be used as a prefix.

Value

This returns an object of matrix which contains indicators.

Examples

1
2
3
4
5
6
a1 = 4:10;
b1 = c("aa", "bb", "cc");

to.indicators(a1, base = 10);
to.indicators(b1, base = "bb", prefix = "T_");
to.indicators(as.data.frame(b1), base = "bb");

ncpen documentation built on May 1, 2019, 9:21 p.m.