Learn R Programming

regtools (version 1.1.0)

factorsToDummies: Factor Conversion Utilities

Description

Utilities from converting back and forth between factors and dummy variables.

Usage

factorToDummies(f,fname,omitLast=TRUE)
factorsToDummies(dfr,omitLast=TRUE)
dummiesToFactor(dms,inclLast=FALSE)

Arguments

f

A factor.

fname

A factor name.

dfr

A data frame.

omitLast

If TRUE, then generate only k-1 dummies from k factor levels.

dms

A data frame whose columns are dummy variables.

inclLast

If FALSE, then only k-1 dummies for k factor levels are provided.

Value

The function factorToDummies returns a matrix of dummy variables, while factorsToDummies returns a new version of the input data frame, in which each factor is replaced by columns of dummies.

Details

Many R users prefer to use R factors in their coding, or work with data that is of this type to begin with. On the other hand, many regression packages, e.g. lars, disallow factors. These utilities facilitate conversion from one form to another.

Examples

Run this code
# NOT RUN {
f <- as.factor(c('abc','de','f','de','abc'))
factorToDummies(f,'f')
# outputs
#      f.abc f.de
# [1,]     1    0
# [2,]     0    1
# [3,]     0    0
# [4,]     0    1
# [5,]     1    0
d <- data.frame(a=1:5,b=f)
factorsToDummies(d,omitLast=FALSE)
# outputs
#   a b.abc b.de
# 1 1     1    0
# 2 2     0    1
# 3 3     0    0
# 4 4     0    1
# 5 5     1    0
# }

Run the code above in your browser using DataLab