Holden
Holden

Reputation: 7442

In R How do I load a matrix market formated sparse matrix into a dgCMatrix?

I'm trying to use the the Matrix package to read a MatrixMarket formatted file, but I get back a ngTMatrix. I can't convert it with as since there is no method for converting from ngTMatrix to dgCMatrix. Converting it to a non-sparse matrix isn't an option as its too large. Any suggestions?

Upvotes: 2

Views: 1620

Answers (1)

Prasad Chalasani
Prasad Chalasani

Reputation: 20282

I'm not sure why you're having a problem, because you can use as() to coerce an ngTMatrix to an ngCMatrix:

> pm <- as(as.integer(c(2,3,1)), "pMatrix")
3 x 3 sparse Matrix of class "pMatrix"

[1,] . | .
[2,] . . |
[3,] | . .

> pm.t <- as(pm, 'ngTMatrix')
> pm.c <- as(pm.t, 'ngCMatrix')

> class(pm.c)
[1] "ngCMatrix"
attr(,"package")
[1] "Matrix"

> class(pm.t)
[1] "ngTMatrix"
attr(,"package")
[1] "Matrix"

Upvotes: 3

Related Questions