mchangun
mchangun

Reputation: 10322

Dummy Variables for Data Frame Containing only Predictors

I know how to create a matrix with dummy variables if the response variable is part of the initial matrix:

trainx = model.matrix(survived ~ . -1, data = train)

However, how can I do it for a matrix that only contains the predictors? It's the test set so there are no predictors. I don't know how the syntax works for that situation.

model.matrix(~test)
Error in model.frame.default(object, data, xlev = xlev) : 
  invalid type (list) for variable 'test'

Have tried various combinations and nothing works. Note test contains columns that are factors as well as numeric.

Thank you.

Upvotes: 2

Views: 287

Answers (1)

Hong Ooi
Hong Ooi

Reputation: 57686

model.matrix(~., data=test)

In other words, omit the LHS of the formula. Don't put the dataset name into the formula itself!

Upvotes: 4

Related Questions