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.
A function to make dummy variables for R.
makedummies(dat, basal_level = FALSE, col = NULL, numerical = NULL, as.is = NULL)
dat <- data.frame(x = factor(rep(c("a", "b", "c"), each = 3)))dat$xmakedummies(dat)
[1] a a a b b b c c cLevels: a b cx_b x_c1 0 02 0 03 0 04 1 05 1 06 1 07 0 18 0 19 0 1
makedummies(dat, basal_level = TRUE)
x_a x_b x_c1 1 0 02 1 0 03 1 0 04 0 1 05 0 1 06 0 1 07 0 0 18 0 0 19 0 0 1
dat <- data.frame(x = factor(rep(c("a", "b", "c"), each = 3)))dat$x <- ordered(dat$x, levels = c("a" ,"c" ,"b"))dat$xmakedummies(dat)
[1] a a a b b b c c cLevels: a < c < bx_c x_b1 0 02 0 03 0 04 0 15 0 16 0 17 1 08 1 09 1 0
dat <- data.frame(x = rep(1:3, each = 3))makedummies(dat)
x1 12 13 14 25 26 27 38 39 3
dat <- data.frame(x = factor(rep(c("a", "b", "c"), each = 3)),y = rep(1:3, each = 3))makedummies(dat)
x_b x_c y1 0 0 12 0 0 13 0 0 14 1 0 25 1 0 26 1 0 27 0 1 38 0 1 39 0 1 3
dat <- data.frame(x = factor(rep(c("a", "b", "c"), each = 3)),y = factor(rep(1:3, each = 3)))makedummies(dat)
x_b x_c y_2 y_31 0 0 0 02 0 0 0 03 0 0 0 04 1 0 1 05 1 0 1 06 1 0 1 07 0 1 0 18 0 1 0 19 0 1 0 1
dat <- data.frame(x = factor(rep(c("a", "b", "c"), each = 3)),y = factor(rep(1:3, each = 3))v)makedummies(dat, col = "x")
x_b x_c1 0 02 0 03 0 04 1 05 1 06 1 07 0 18 0 19 0 1
dat <- data.frame(x = factor(rep(c("a", "b", "c"), each = 3)),y = factor(rep(1:3, each = 3)))makedummies(dat, numerical = "x")
x y_2 y_31 1 0 02 1 0 03 1 0 04 2 1 05 2 1 06 2 1 07 3 0 18 3 0 19 3 0 1
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"))datdat$xmakedummies(dat, numerical = c("x", "y"))
x y1 a 42 a 43 a 44 b 55 b 56 b 57 c 68 c 69 c 6[1] a a a b b b c c cLevels: a < c < bx y1 1 42 1 43 1 44 3 55 3 56 3 57 2 68 2 69 2 6
dat <- data.frame(x = factor(rep(c("a", "b", "c"), each = 3)),y = factor(rep(1:3, each = 3)))makedummies(dat, as.is = "x")
x y_2 y_31 a 0 02 a 0 03 a 0 04 b 1 05 b 1 06 b 1 07 c 0 18 c 0 19 c 0 1
makedummies(dat, as.is = c("x", "y"))
x y1 a 12 a 13 a 14 b 25 b 26 b 27 c 38 c 39 c 3
Changes to version 1.2.0
matrix
class data is automatically converted to data.frame
classChanges to version 1.1.2
Changes to version 1.1.1
Changes to version 1.1