Reputation: 31
For example. A factor that with ordered levels
[1] 0 0 6 6 3 4
Levels: 0 < 1 < 2 < 3 < 4 < 5 < 6
should be converted to
ti0 ti1 ti2 ti3 ti4 ti5 ti6
1 0 0 0 0 0 0
1 0 0 0 0 0 0
1 1 1 1 1 1 1
1 1 1 1 1 1 1
1 1 1 1 0 0 0
1 1 1 1 1 0 0
I have looked at packages like dummies
and functions like model model.matrix
but cant get to a solution.
Upvotes: 0
Views: 784
Reputation: 3883
If your purpose is to create a variable with a custom contrast matrix, and in your case it looks like you are creating a cumulatively coded ordinal variable as described here, you can do the following.
x <- factor(c("0", "0", "6", "6", "3", "4"), levels = 0:6, ordered = TRUE)
# Create custom contrast function
contr.cum <- function(x, base = 1L)
{
dmns <- levels(x)
n <- length(dmns)
contr <- array(diag(n), dim = c(n,n), dimnames = list(dmns, dmns))
contr[lower.tri(contr)] <- 1
contr <- contr[, -base, drop = FALSE]
contr
}
# Apply custom function to variable
contrasts(x) <- contr.cum(x)
# View model matrix
model.matrix(~x)
This 'customised' variable can be used directly in other functions (eg. regression equations) without the need to manually create dummy variables.
Upvotes: 0
Reputation: 70653
This appears to work.
x <- factor(c("0", "0", "6", "6", "3", "4"), levels = 0:6, ordered = TRUE)
out <- matrix(0, nrow = length(x), ncol = max(as.numeric(x)))
for (i in 1:length(x)) {
out[i, 1:as.numeric(x[i])] <- 1
}
colnames(out) <- paste("ti", levels(x), sep = "")
ti0 ti1 ti2 ti3 ti4 ti5 ti6
[1,] 1 0 0 0 0 0 0
[2,] 1 0 0 0 0 0 0
[3,] 1 1 1 1 1 1 1
[4,] 1 1 1 1 1 1 1
[5,] 1 1 1 1 0 0 0
[6,] 1 1 1 1 1 0 0
Upvotes: 1