makedummies: Create Dummy Variables from Categorical Data

Description Usage Arguments Value Note Examples

View source: R/makedummies.R

Description

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.

Usage

1
2
makedummies(dat, basal_level = FALSE, col = NULL, numerical = NULL,
  as.is = NULL)

Arguments

dat

data.frame

basal_level
TRUE

: include a dummy variable for base group

FALSE

(default) : exclude a dummy variable for base group

col

Columns vector (all columns are used if NULL is given)

numerical

Columns vector converting from factor/ordered to numeric (ignore if column is numeric)

as.is

Columns vector not converting

Value

return as data.frame

Note

tbl class is also accepted from version 1.1 (returned as data.frame).

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.

Examples

 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
## 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)

## "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"))

toshi-ara/makedummies documentation built on Nov. 15, 2018, 9:48 p.m.