coding: Dummy Coding

Description Usage Arguments Details Value Warning Author(s) See Also Examples

Description

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

Usage

1
2
3
4
5
6
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

1
2
3
4
5
6
7
### 
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)

Example output

Loading required package: grid
Loading required package: proxy

Attaching package: 'proxy'

The following objects are masked from 'package:stats':

    as.dist, dist

The following object is masked from 'package:base':

    as.matrix

       [,1]  [,2]  [,3]
 [1,] FALSE  TRUE FALSE
 [2,] FALSE FALSE  TRUE
 [3,] FALSE  TRUE FALSE
 [4,] FALSE FALSE  TRUE
 [5,]  TRUE FALSE FALSE
 [6,]  TRUE FALSE FALSE
 [7,] FALSE FALSE  TRUE
 [8,] FALSE  TRUE FALSE
 [9,] FALSE FALSE  TRUE
[10,] FALSE  TRUE FALSE
attr(,"levels")
[1] "1" "2" "3"
       [,1]  [,2]  [,3]
 [1,] FALSE  TRUE FALSE
 [2,] FALSE FALSE  TRUE
 [3,] FALSE FALSE FALSE
 [4,] FALSE FALSE  TRUE
 [5,] FALSE FALSE FALSE
 [6,]  TRUE FALSE FALSE
 [7,] FALSE FALSE  TRUE
 [8,] FALSE  TRUE FALSE
 [9,] FALSE FALSE  TRUE
[10,] FALSE  TRUE FALSE
attr(,"levels")
[1] "1" "2" "3"
     x 1   x 2   x 3
1  FALSE  TRUE FALSE
2  FALSE FALSE  TRUE
3  FALSE FALSE FALSE
4  FALSE FALSE  TRUE
5  FALSE FALSE FALSE
6   TRUE FALSE FALSE
7  FALSE FALSE  TRUE
8  FALSE  TRUE FALSE
9  FALSE FALSE  TRUE
10 FALSE  TRUE FALSE
attr(,"levels")
attr(,"levels")$x
[1] "1" "2" "3"

cba documentation built on May 2, 2019, 1:39 p.m.

Related to coding in cba...