| Dummy {DescTools} | R Documentation |
Generates a matrix of dummy codes (class indicators) for a given factor.
Dummy(x, method = c("treatment", "sum", "helmert", "poly", "full"),
base = 1, levels = NULL)
x |
factor or vector of classes for cases. |
method |
defines the method of the contrasts being formed. Can be one
out of |
base |
an integer specifying which group is considered the baseline group. |
levels |
an optional vector of the values (as character strings) that |
a matrix with the dummy codes.
The number of rows correspond to the number of elements in x and the number of columns to the number of its levels - 1, respectively to the number of levels given as argument -1.
When method full is chosen the number of columns will correspond to the number of levels.
Andri Signorell <andri@signorell.net>
Venables, W N and Ripley, B D (2002): Modern Applied Statistics with S. Fourth edition. Springer.
model.frame, contrasts, class.ind in the package nnet
x <- c("red","blue","green","blue","green","red","red","blue")
Dummy(x)
Dummy(x, base = 2)
Dummy(x, method = "sum")
y <- c("Max","Max","Max","Max","Max","Bill","Bill","Bill")
Dummy(y)
Dummy(y, base = "Max")
Dummy(y, base = "Max", method="full")
# "Undummy" (revert the dummy coding)
m <- Dummy(y, method="full")
m
z <- apply(m, 1, function(x) colnames(m)[x==1])
z
identical(y, as.vector(z))
m <- Dummy(y)
m
z <- apply(m, 1, function(x) ifelse(sum(x)==0, attr(m,"base"), colnames(m)[x==1]))
z