coding | R Documentation |
Functions that convert R objects to a dummy coded matrix (or a list of matrices).
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, ...)
x |
an R object (see below). |
sep |
separator used for construction of colnames. |
drop |
drop factors with less than two levels. |
... |
other (unused) arguments. |
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.
A matrix
with a levels
attribute which contains a list of
the levels of the coded variables.
This is experimental code which may change in the future.
Christian Buchta
as.logical
.
###
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.