Suppose you have a data frame with a high number of columns(1000 factors, each with 15 levels). You\'d like to create a dummy variable data set, but since it would be too sp
This can be done slightly more compactly with Matrix:::sparse.model.matrix,
although the requirement to have all columns for all variables makes things
a little more difficult.
Generate input:
set.seed(123)
n <- 6
df <- data.frame(x = sample(c("A", "B", "C"), n, TRUE),
y = sample(c("D", "E"), n, TRUE))
If you didn't need all columns for all variables you could just do:
library(Matrix)
sparse.model.matrix(~.-1,data=df)
If you need all columns:
fList <- lapply(names(df),reformulate,intercept=FALSE)
mList <- lapply(fList,sparse.model.matrix,data=df)
do.call(cBind,mList)