G FANG
2010-Jul-06 19:46 UTC
[R] Get the indices of non-zero entries of a sparse matrix in R
Hi,
I am trying to get the indices of non-zero entries of a sparse matrix in R
s r d
1 1089 3772 1
2 1109 190 1
3 1109 2460 1
4 1109 3071 1
5 1109 3618 1
6 1109 38 1
I found that the following can create a sparse matrix,
library(Matrix)
Y <- sparseMatrix(s,r,x=d)
but have not idea and did not find online how to convert a sparse
matrix back to three columns efficiently, i.e. get the indices of
non-zero entries of a sparse matrix.
In matlab, the find function can do this efficiently, I am wondering
is there a similar one in R?
Please advice. Thanks!
--gang
David Winsemius
2010-Jul-06 20:59 UTC
[R] Get the indices of non-zero entries of a sparse matrix in R
On Jul 6, 2010, at 3:46 PM, G FANG wrote:> Hi, > > I am trying to get the indices of non-zero entries of a sparse > matrix in R > > s r d > 1 1089 3772 1 > 2 1109 190 1 > 3 1109 2460 1 > 4 1109 3071 1 > 5 1109 3618 1 > 6 1109 38 1 > > I found that the following can create a sparse matrix, > > library(Matrix) > Y <- sparseMatrix(s,r,x=d) > > but have not idea and did not find online how to convert a sparse > matrix back to three columns efficiently, i.e. get the indices of > non-zero entries of a sparse matrix. > > In matlab, the find function can do this efficiently, I am wondering > is there a similar one in R?Yes there is, and it was reasonably easy to find with the help systems starting from the Matrix help page. ?sparseMatrix summary(Y) -- David Winsemius, MD West Hartford, CT