dummyCodeFactorDf: Dummy coding of a data frame of factor variables

Description Usage Arguments Value Examples

View source: R/misc_functions.R

Description

Given a data frame of factor variables, this function returns a numeric matrix of 0–1 dummy-coded variables.

Usage

1

Arguments

dat

A data frame of factor variables

Value

A numeric matrix of 0–1 dummy coded variables

Examples

1
2
dd <- data.frame(a=factor(1:8), b=factor(letters[1:8]), stringsAsFactors = TRUE)
dummyCodeFactorDf(dd)

Example output

     a_1 a_2 a_3 a_4 a_5 a_6 a_7 a_8 b_a b_b b_c b_d b_e b_f b_g b_h
[1,]   1   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0
[2,]   0   1   0   0   0   0   0   0   0   1   0   0   0   0   0   0
[3,]   0   0   1   0   0   0   0   0   0   0   1   0   0   0   0   0
[4,]   0   0   0   1   0   0   0   0   0   0   0   1   0   0   0   0
[5,]   0   0   0   0   1   0   0   0   0   0   0   0   1   0   0   0
[6,]   0   0   0   0   0   1   0   0   0   0   0   0   0   1   0   0
[7,]   0   0   0   0   0   0   1   0   0   0   0   0   0   0   1   0
[8,]   0   0   0   0   0   0   0   1   0   0   0   0   0   0   0   1

kamila documentation built on March 13, 2020, 9:08 a.m.