Learn R Programming

cba (version 0.2-5)

coding: Dummy Coding

Description

Functions that convert R objects to a dummy coded matrix (or a list of matrices).

Usage

as.dummy(x, ...)

## S3 method for class 'matrix': as.dummy(x, sep = " ", drop = FALSE, ...) ## S3 method for class 'data.frame': as.dummy(x, sep = " ", drop = FALSE, ...)

Arguments

x
an R object (see below).
sep
separator used for construction of colnames.
drop
drop factors with less than two levels.
...
other (unused) arguments.

Value

  • A matrix with a levels attribute which contains a list of the levels of the coded variables.

Warning

This is experimental code which may change in the future.

Details

The generic is applicable to factor and to R objects that can be converted to factor, i.e. logical, integer, or character. For numeric data a discretization method has to be used.

A factor is converted to as many logical variables as there are levels where the value TRUE indicates the presence of a level.

The colnames are made of the concatenation of a variable name and the level, separated by sep. For matrix and data.frame variable names are created if necessary.

A value of NA is mapped to FALSE across all levels.

See Also

as.logical.

Examples

Run this code
### 
x <- as.integer(sample(3,10,rep=TRUE))
as.dummy(x)
is.na(x) <- c(3,5)
as.dummy(x)
x <- as.data.frame(x,x)
as.dummy(x)

Run the code above in your browser using DataLab