Skip to content
Prev 106728 / 398506 Next

coded to categorical variables in a large dataset

As Richard has already pointed out you may only need to convert your
numeric vector to a factor but just in case here are a few direct answers:


Using X from Chuck's post here are two ways of creating a 100x5
matrix of indicator variables:

model.matrix(~ X-1, list(X = factor(X)))
outer(X, 1:5, "==")+0

# To create eventi variables
# here is a way of creating them

event1 <- (X == 1) + 0 # and similarly for 2, 3, 4, 5

# or do it in a loop
for(i in 1:5) assign(paste("event", i, sep = ""), (X == i) + 0)

# or create as columns of a data frame
f <- function(i, j) (X == j) + 0
as.data.frame(mapply(f, paste("event", 1:5, sep = ""), 1:5))
On 12/29/06, sj <ssj1364 at gmail.com> wrote: