make.dummy {hgwrr} | R Documentation |
Make Dummy Variables
Description
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. |
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.
Value
The data frame with extracted dummy variables.
Examples
make.dummy(iris["Species"])
make.dummy.extract(iris$Species, "Species")
make.dummy.extract(c("top", "mid", "low", "mid", "top"), "level")
make.dummy.extract(factor(c("far", "near", "near")), "distance")
make.dummy.extract(c(TRUE, TRUE, FALSE), "sold")