dat <- data.frame(x = c(1, 1, 1, 2, 2, 2, 3, 3, 3),
y = c("a", "a", "a", "b", "b", "b", "c", "c", "c"),
z = factor(c("B", "B", "B", "A", "A", "A", "C", "C", "C")),
stringsAsFactors = FALSE)
# Dummy coding of a numeric variable, reference = 3
dummy.c(dat$x)
# Dummy coding of a numeric variable, reference = 1
dummy.c(dat$x, ref = 1)
# Dummy coding of a numeric variable, reference = 3
# assign user-specified variable names
dummy.c(dat$x, names = c("x.3_1", "x.3_2"))
# Dummy coding of a numeric variable, reference = 3
# assign user-specified variable names and attach to the data frame
dat <- data.frame(dat, dummy.c(dat$x, names = c("x.3_1", "x.3_2")), stringsAsFactors = FALSE)
# Dummy coding of a character variable, reference = "c"
dummy.c(dat$y)
# Dummy coding of a character variable, reference = "a"
dummy.c(dat$y, ref = "a")
# Dummy coding of a numeric variable, reference = "c"
# assign user-specified variable names
dummy.c(dat$y, names = c("y.c_a", "y.c_b"))
# Dummy coding of a character variable, reference = "c"
# assign user-specified variable names and attach to the data frame
dat <- data.frame(dat, dummy.c(dat$y, names = c("y.c_a", "y.c_b")), stringsAsFactors = FALSE)
# Dummy coding of a factor, reference = "C"
dummy.c(dat$z)
# Dummy coding of a factor, reference = "A"
dummy.c(dat$z, ref = "A")
# Dummy coding of a numeric variable, reference = "C"
# assign user-specified variable names
dummy.c(dat$z, names = c("z.C_A", "z.C_B"))
# Dummy coding of a factor, reference = "C"
# assign user-specified variable names and attach to the data frame
dat <- data.frame(dat, dummy.c(dat$z, names = c("z.C_A", "z.C_B")), stringsAsFactors = FALSE)
Run the code above in your browser using DataLab