dummy.c | R Documentation |
This function creates k - 1
dummy coded 0/1 variables for a vector with
k distinct values.
dummy.c(x, ref = NULL, names = "d", as.na = NULL, check = TRUE)
x |
a numeric vector with integer values, character vector or factor. |
ref |
a numeric value or character string indicating the reference group. By default, the last category is selected as reference group. |
names |
a character string or character vector indicating the names
of the dummy variables. By default, variables are named |
as.na |
a numeric vector indicating user-defined missing values,
i.e. these values are converted to |
check |
logical: if |
Returns a matrix with k - 1 dummy coded 0/1 variables.
Takuya Yanagida takuya.yanagida@univie.ac.at
Rasch, D., Kubinger, K. D., & Yanagida, T. (2011). Statistics in psychology - Using R and SPSS. New York: John Wiley & Sons.
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.