Uncommon error message converting Matrix to Sparse in R

Viewed 1743

I'm trying to run a LASSO on our dataset, and to do so, I need to convert non-numeric variables to numeric, ideally via a sparse matrix. However, when I try to use the Matrix command, I get the same error:

Error in asMethod(object) : invalid class 'NA' to dup_mMatrix_as_geMatrix

I thought this was due to NA's in my data, so I did an na.omit and got the same error. I tried again with a mini subset of my code and got the same error again:

> sparsecombined <- Matrix(combined1[1:10,],sparse=TRUE)
Error in asMethod(object) : invalid class 'NA' to dup_mMatrix_as_geMatrix

This is the data set I tried to convert with that last line of code:

enter image description here

Is there anything that jumps out that might prevent sparse conversion?

3 Answers

I got this error due to passing a data frame where a matrix was expected, and it looks like that's the same reason you are getting it. The solution in simple -- convert your data to a matrix before passing it to the Matrix function:

sparsecombined <- Matrix(as.matrix(combined1[1:10,]),sparse=TRUE)

In your case, this code will probably complain because you have some non-numeric data stored in there (e.g. the TailNum column). So you would need to downselect to just the numeric columns.

Related