R: Dummy Coding

coding {cba} | R Documentation

## 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.

### 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.

### 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.

### Author(s)

Christian Buchta

### See Also

`as.logical`.

### Examples

    ###
    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)
    as.dummy(x)

[Package cba version 0.2-21 Index]