Learn R Programming

sivipm (version 1.1-4.2)

factorsplit: Transform Categorical Variables into Numeric Variables

Description

Transform each factor column of a dataframe into numeric variables.

  • If the factor has more than 2 levels, it is split into as many 0-1 columns as levels. The value 1 is set on the lines where the factor is equal to the level, and 0 elsewhere.

  • If the factor has one or two levels, the first level is coded -1 and the second one into +1.

The numeric columns are left unchanged.

Usage

factorsplit(data)

Arguments

data

Data.frame.

Value

The data.frame data, where the factor columns are replaced by 0/1 or +1/-1 columns. The labels of the added columns are the concatenation of the name of the factor and the name of the level, with the underscore separator.

See Also

crpolyX, vect2polyX

Examples

Run this code
# NOT RUN {
a <- data.frame(V=1:3, color=c("red","green","black"),
                       temp=c("hot", "cold", "hot"))
b <- factorsplit(a)
# The columns in b are: V, color_red, color_green,  color_red, temp
# }

Run the code above in your browser using DataLab