[R] find indices of nonzero elements

Agustin Lobo alobo at ija.csic.es
Tue Oct 9 10:14:03 CEST 2001


> x <- cbind(c(1,0,0),c(0,0,1),c(0,1,0))
> x
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    0    1
[3,]    0    1    0

> cbind(row(x)[x!=0],col(x)[x!=0])
     [,1] [,2]
[1,]    1    1
[2,]    3    2
[3,]    2    3

Agus
Dr. Agustin Lobo
Instituto de Ciencias de la Tierra (CSIC)
Lluis Sole Sabaris s/n
08028 Barcelona SPAIN
tel 34 93409 5410
fax 34 93411 0012
alobo at ija.csic.es


On Mon, 8 Oct 2001, David A Richmond wrote:

> Hi,
> 	Is there a function func(x) where x is an array such that it
> returns a list of the indices of all non-zero elements of the array?
> 
> for example:
> a:
> 1 0 0 
> 0 0 1
> 0 1 0
> 
> func(a) returns two vectors r and c:
> r c
> 1 1
> 2 3
> 3 2
> 
> 
> daver
> 
> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> |David Richmond                It works on a          |      
> + Dept. of Sociology          complex scientific      + 
> |Saint Mary's College          principle, known as    |  
> + Notre Dame, IN 46556               "pot luck."      +
> |219-284-4517                    - The Doctor         |
> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list