Description Usage Arguments Value Note Examples
Create dummy variables from categorical data.
This package can convert categorical data (factor and ordered) into
dummy variables and handle multiple columns simultaneously.
This package enables to select whether a dummy variable for base group
is included (for principal component analysis/factor analysis) or
excluded (for regression analysis) by an option.
makedummies
function accepts
data.frame
, matrix
, and
tbl
(tibble) class (by tibble
package).
matrix
class data is automatically converted to
data.frame
class.
1 2 3 4 5 6 7 8 9 10 11 12 | makedummies(dat, ...)
## Default S3 method:
makedummies(dat, basal_level = FALSE, col = NULL,
numerical = NULL, as.is = NULL, ...)
## S3 method for class 'matrix'
makedummies(dat, ...)
## S3 method for class 'tbl'
makedummies(dat, basal_level = FALSE, col = NULL,
numerical = NULL, as.is = NULL, ...)
|
dat |
data of |
... |
arguments to makedummies.data.frame ( |
basal_level |
logical
|
col |
Columns vector (all columns are used if |
numerical |
Columns vector converting from |
as.is |
Columns vector not converting |
return as data.frame
or tbl
class
Pull Request #1 (add column name when when columns has binary value) (https://github.com/toshi-ara/makedummies/pull/1). Thanks to Kohki YAMAGIWA for the contribution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | #### 'data.frame' class
## factor
dat <- data.frame(x = factor(rep(c("a", "b", "c"), each = 3)))
dat$x
makedummies(dat)
## ordered
dat <- data.frame(x = factor(rep(c("a", "b", "c"), each = 3)))
dat$x <- ordered(dat$x, levels = c("a" ,"c" ,"b"))
dat$x
makedummies(dat)
## numeric
dat <- data.frame(x = rep(1:3, each = 3))
makedummies(dat)
## factor and numeric
dat <- data.frame(
x = factor(rep(c("a", "b", "c"), each = 3)),
y = rep(1:3, each = 3)
)
makedummies(dat)
## factors
dat <- data.frame(
x = factor(rep(c("a", "b", "c"), each = 3)),
y = factor(rep(1:3, each = 3))
)
makedummies(dat)
## data including NA
dat <- data.frame(
x = factor(rep(c("a", "b", "c"), each = 3)),
y = rep(1:3, each = 3)
)
dat$x[4] <- NA; dat$y[6] <- NA
makedummies(dat)
## "col" option
dat <- data.frame(
x = factor(rep(c("a", "b", "c"), each = 3)),
y = factor(rep(1:3, each = 3))
)
makedummies(dat, col = "x")
## "numerical" option
dat <- data.frame(
x = factor(rep(c("a", "b", "c"), each = 3)),
y = factor(rep(1:3, each = 3))
)
makedummies(dat, numeric = "x")
dat <- data.frame(
x = factor(rep(c("a", "b", "c"), each = 3)),
y = rep(4:6, each = 3)
)
dat$x <- ordered(dat$x, levels = c("a" ,"c" ,"b"))
dat
dat$x
makedummies(dat, numeric = c("x", "y"))
## "as.is" option
dat <- data.frame(
x = factor(rep(c("a", "b", "c"), each = 3)),
y = factor(rep(1:3, each = 3))
)
dat
makedummies(dat, as.is = "x")
makedummies(dat, as.is = c("x", "y"))
#### 'tibble' class
if (require(tibble)) {
dat <- as_tibble(iris)
makedummies(dat[46:55,], col = "Species", basal_level = TRUE)
# non-standard variable name
dat2 <- tibble(
`1` = factor(rep(c("c", "a", "b"), each = 3)),
`@` = factor(rep(1:3, each = 3)),
`&` = rep(4:6, each = 3)
)
dat2
makedummies(dat2, basal_level = TRUE)
makedummies(dat2, as.is = "@", basal_level = TRUE)
makedummies(dat2, numerical = "1", basal_level = TRUE)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.