Skip to contents

Function make.dummy converts categorical variables in a data frame to dummy variables.

Function make.dummy.extract converts a column to dummy variables if necessary and assign appropriate names. See the "detail" section for further information. Users can define their own functions to allow the model deal with some types of variables properly.

Usage

make.dummy(data)

make.dummy.extract(col, name)

# S3 method for class 'character'
make.dummy.extract(col, name)

# S3 method for class 'factor'
make.dummy.extract(col, name)

# S3 method for class 'logical'
make.dummy.extract(col, name)

# Default S3 method
make.dummy.extract(col, name)

Arguments

data

The data frame from which dummy variables need to be extracted.

col

A vector to extract dummy variables.

name

The vector's name.

Value

The data frame with extracted dummy variables.

Details

If col is a character vector, the function will get unique values of its elements and leave out the last one. Then, all the unique values are combined with the name argument as names of new columns.

If col is a factor vector, the function will get its levels and leave out the last one. Then, all level labels are combined with the name argument as names of new columns.

If col is a logical vector, the function will convert it to a numeric vector with value TRUE mapped to 1 and FALSE to 0.

If col is of other types, the default behaviour for extracting dummy variables is just to copy the original value and try to convert it to numeric values.

Examples

make.dummy(iris["Species"])
#>     Species.setosa Species.versicolor
#> 1                1                  0
#> 2                1                  0
#> 3                1                  0
#> 4                1                  0
#> 5                1                  0
#> 6                1                  0
#> 7                1                  0
#> 8                1                  0
#> 9                1                  0
#> 10               1                  0
#> 11               1                  0
#> 12               1                  0
#> 13               1                  0
#> 14               1                  0
#> 15               1                  0
#> 16               1                  0
#> 17               1                  0
#> 18               1                  0
#> 19               1                  0
#> 20               1                  0
#> 21               1                  0
#> 22               1                  0
#> 23               1                  0
#> 24               1                  0
#> 25               1                  0
#> 26               1                  0
#> 27               1                  0
#> 28               1                  0
#> 29               1                  0
#> 30               1                  0
#> 31               1                  0
#> 32               1                  0
#> 33               1                  0
#> 34               1                  0
#> 35               1                  0
#> 36               1                  0
#> 37               1                  0
#> 38               1                  0
#> 39               1                  0
#> 40               1                  0
#> 41               1                  0
#> 42               1                  0
#> 43               1                  0
#> 44               1                  0
#> 45               1                  0
#> 46               1                  0
#> 47               1                  0
#> 48               1                  0
#> 49               1                  0
#> 50               1                  0
#> 51               0                  1
#> 52               0                  1
#> 53               0                  1
#> 54               0                  1
#> 55               0                  1
#> 56               0                  1
#> 57               0                  1
#> 58               0                  1
#> 59               0                  1
#> 60               0                  1
#> 61               0                  1
#> 62               0                  1
#> 63               0                  1
#> 64               0                  1
#> 65               0                  1
#> 66               0                  1
#> 67               0                  1
#> 68               0                  1
#> 69               0                  1
#> 70               0                  1
#> 71               0                  1
#> 72               0                  1
#> 73               0                  1
#> 74               0                  1
#> 75               0                  1
#> 76               0                  1
#> 77               0                  1
#> 78               0                  1
#> 79               0                  1
#> 80               0                  1
#> 81               0                  1
#> 82               0                  1
#> 83               0                  1
#> 84               0                  1
#> 85               0                  1
#> 86               0                  1
#> 87               0                  1
#> 88               0                  1
#> 89               0                  1
#> 90               0                  1
#> 91               0                  1
#> 92               0                  1
#> 93               0                  1
#> 94               0                  1
#> 95               0                  1
#> 96               0                  1
#> 97               0                  1
#> 98               0                  1
#> 99               0                  1
#> 100              0                  1
#> 101              0                  0
#> 102              0                  0
#> 103              0                  0
#> 104              0                  0
#> 105              0                  0
#> 106              0                  0
#> 107              0                  0
#> 108              0                  0
#> 109              0                  0
#> 110              0                  0
#> 111              0                  0
#> 112              0                  0
#> 113              0                  0
#> 114              0                  0
#> 115              0                  0
#> 116              0                  0
#> 117              0                  0
#> 118              0                  0
#> 119              0                  0
#> 120              0                  0
#> 121              0                  0
#> 122              0                  0
#> 123              0                  0
#> 124              0                  0
#> 125              0                  0
#> 126              0                  0
#> 127              0                  0
#> 128              0                  0
#> 129              0                  0
#> 130              0                  0
#> 131              0                  0
#> 132              0                  0
#> 133              0                  0
#> 134              0                  0
#> 135              0                  0
#> 136              0                  0
#> 137              0                  0
#> 138              0                  0
#> 139              0                  0
#> 140              0                  0
#> 141              0                  0
#> 142              0                  0
#> 143              0                  0
#> 144              0                  0
#> 145              0                  0
#> 146              0                  0
#> 147              0                  0
#> 148              0                  0
#> 149              0                  0
#> 150              0                  0

make.dummy.extract(iris$Species, "Species")
#>     Species.setosa Species.versicolor
#> 1                1                  0
#> 2                1                  0
#> 3                1                  0
#> 4                1                  0
#> 5                1                  0
#> 6                1                  0
#> 7                1                  0
#> 8                1                  0
#> 9                1                  0
#> 10               1                  0
#> 11               1                  0
#> 12               1                  0
#> 13               1                  0
#> 14               1                  0
#> 15               1                  0
#> 16               1                  0
#> 17               1                  0
#> 18               1                  0
#> 19               1                  0
#> 20               1                  0
#> 21               1                  0
#> 22               1                  0
#> 23               1                  0
#> 24               1                  0
#> 25               1                  0
#> 26               1                  0
#> 27               1                  0
#> 28               1                  0
#> 29               1                  0
#> 30               1                  0
#> 31               1                  0
#> 32               1                  0
#> 33               1                  0
#> 34               1                  0
#> 35               1                  0
#> 36               1                  0
#> 37               1                  0
#> 38               1                  0
#> 39               1                  0
#> 40               1                  0
#> 41               1                  0
#> 42               1                  0
#> 43               1                  0
#> 44               1                  0
#> 45               1                  0
#> 46               1                  0
#> 47               1                  0
#> 48               1                  0
#> 49               1                  0
#> 50               1                  0
#> 51               0                  1
#> 52               0                  1
#> 53               0                  1
#> 54               0                  1
#> 55               0                  1
#> 56               0                  1
#> 57               0                  1
#> 58               0                  1
#> 59               0                  1
#> 60               0                  1
#> 61               0                  1
#> 62               0                  1
#> 63               0                  1
#> 64               0                  1
#> 65               0                  1
#> 66               0                  1
#> 67               0                  1
#> 68               0                  1
#> 69               0                  1
#> 70               0                  1
#> 71               0                  1
#> 72               0                  1
#> 73               0                  1
#> 74               0                  1
#> 75               0                  1
#> 76               0                  1
#> 77               0                  1
#> 78               0                  1
#> 79               0                  1
#> 80               0                  1
#> 81               0                  1
#> 82               0                  1
#> 83               0                  1
#> 84               0                  1
#> 85               0                  1
#> 86               0                  1
#> 87               0                  1
#> 88               0                  1
#> 89               0                  1
#> 90               0                  1
#> 91               0                  1
#> 92               0                  1
#> 93               0                  1
#> 94               0                  1
#> 95               0                  1
#> 96               0                  1
#> 97               0                  1
#> 98               0                  1
#> 99               0                  1
#> 100              0                  1
#> 101              0                  0
#> 102              0                  0
#> 103              0                  0
#> 104              0                  0
#> 105              0                  0
#> 106              0                  0
#> 107              0                  0
#> 108              0                  0
#> 109              0                  0
#> 110              0                  0
#> 111              0                  0
#> 112              0                  0
#> 113              0                  0
#> 114              0                  0
#> 115              0                  0
#> 116              0                  0
#> 117              0                  0
#> 118              0                  0
#> 119              0                  0
#> 120              0                  0
#> 121              0                  0
#> 122              0                  0
#> 123              0                  0
#> 124              0                  0
#> 125              0                  0
#> 126              0                  0
#> 127              0                  0
#> 128              0                  0
#> 129              0                  0
#> 130              0                  0
#> 131              0                  0
#> 132              0                  0
#> 133              0                  0
#> 134              0                  0
#> 135              0                  0
#> 136              0                  0
#> 137              0                  0
#> 138              0                  0
#> 139              0                  0
#> 140              0                  0
#> 141              0                  0
#> 142              0                  0
#> 143              0                  0
#> 144              0                  0
#> 145              0                  0
#> 146              0                  0
#> 147              0                  0
#> 148              0                  0
#> 149              0                  0
#> 150              0                  0

make.dummy.extract(c("top", "mid", "low", "mid", "top"), "level")
#>   level.top level.mid
#> 1         1         0
#> 2         0         1
#> 3         0         0
#> 4         0         1
#> 5         1         0

make.dummy.extract(factor(c("far", "near", "near")), "distance")
#>   distance.far
#> 1            1
#> 2            0
#> 3            0

make.dummy.extract(c(TRUE, TRUE, FALSE), "sold")
#>   sold
#> 1    1
#> 2    1
#> 3    0