make_dummy | R Documentation |
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.
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)
data |
The data frame from which dummy variables need to be extracted. |
col |
A vector to extract dummy variables. |
name |
The vector's name. |
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.
The data frame with extracted dummy variables.
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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.