I have a data.frame consisting of numeric and factor variables, as shown below.
testFrame <- data.frame(First=sample(1:10, 20, replace=T), Second=sample(1:20, 20, replace=T), Third=sample(1:10, 20, replace=T), Fourth=rep(c("Alice","Bob","Charlie","David"), 5), Fifth=rep(c("Edward","Frank","Georgia","Hank","Isaac"),4))
I want to build a matrix that assigns factorial variables to a factor and leaves only numeric variables.
model.matrix(~ First + Second + Third + Fourth + Fifth, data=testFrame)
As expected, when running lm this eliminates one level of each factor as a reference level. However, I want to build a matrix with a dummy / indicator variable for each level of all factors. I am glmnet this matrix for glmnet , so I don't care about multicollinearity.
Is there a way to have a model.matrix create a dummy for each factor level?
matrix r model
Jared Dec 30 '10 at 6:18 2010-12-30 06:18
source share