Learn R Programming

StatMatch (version 1.2.0)

fact2dummy: Transforms a categorical variable in a set of dummy variables

Description

Transforms a factor or more factors contained in a data frame in a set of dummy variables.

Usage

fact2dummy(data, all=TRUE, lab="x")

Arguments

data
A factor or a data frame that contains one or more factors (columns whose class is factor or ordered) that have to be substituted by dummy variables.
all
Logical. When all=TRUE (default) the output matrix will contain as many dummy variables as the number of the levels of the factor variable. On the contrary, when all=FALSE, the dummy variable related to the last level of the fact
lab
A character string with the name of the variable to be pasted with its levels. This is used only when data is a factor. By default it is set to x.

Value

  • A matrix with the dummy variables instead of initial factor variables.

Details

This function substitutes categorical variables in the input data frame (columns whose class is factor or ordered) with the corresponding dummy variables.

See Also

gower.dist

Examples

Run this code
x <- runif(5)
y <- factor(c(1,2,1,2,2))
z <- ordered(c(1,2,3,2,2))
xyz <- data.frame(x,y,z)
fact2dummy(xyz)

fact2dummy(xyz, all=FALSE)


#example with iris data frame
str(iris)
ir.mat <- fact2dummy(iris)

Run the code above in your browser using DataLab