Error in model.frame.default(object, data, xlev = xlev) : object is not a matrix

Faller picture Faller · Nov 18, 2016 · Viewed 20.2k times · Source

3 days old to R and can't figure out what I'm doing wrong. I'm trying to send some columns with two way interactions into a glmnet cox model. I have some data.frame() called dtable

Edit to make the code reproducible

xs<-c("Col1", "Col2", "Col3")
v<-c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, NA, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, NA, 25, 26, 27, 28, 29, 30)
df<-data.frame(matrix(v,ncol=3))
dm<-as.matrix(df)
dm<-matrix(dm[complete.cases(dm)], ncol=3)
colnames(dm)<-xs
dfdata<-data.frame(dm)
f<-as.formula(time~.*.)
xmatrix<-model.matrix(f, dfdata)[,-1]

When I run this I get the error

Error in model.frame.default(object, data, xlev = xlev) :    
object is not a matrix

Thanks in advance

Answer

Faller picture Faller · Nov 18, 2016

It's because of the formula: time~.*. In the data.frame() there is no time column for the formula to cross everything by.