[R] basic array question

Petr Pikal petr.pikal at precheza.cz
Fri Jun 2 10:51:20 CEST 2006


Hi

On 2 Jun 2006 at 10:26, Uwe Ligges wrote:

Date sent:      	Fri, 02 Jun 2006 10:26:44 +0200
From:           	Uwe Ligges <ligges at statistik.uni-dortmund.de>
Organization:   	Fachbereich Statistik, Universitaet Dortmund
To:             	Tim Alcon <talcon at iastate.edu>
Copies to:      	r-help at stat.math.ethz.ch
Subject:        	Re: [R] basic array question

> Tim Alcon wrote:
> > I have a large array and would like to extract from it the row and
> > column indices just of values for which a particular boolean
> > condition 
> 
> Do you mean a matrix, i.e. exactly 2 dimensions?
> 
> > is true.  I assume there's a simple way to do this, but I haven't
> > figured it out yet.  Any help would be appreciated.
> 
> Example:
> 
> 
>   X <- matrix(1:9, 3)
>   col(X)[X == 4]
>   row(X)[X == 4]
>

or

> which(X==4, arr.ind=T)
     row col
[1,]   1   2
>
which works on higher dim arrays too

> X <- array(1:12, c(3,2,2))

> X
, , 1

     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

, , 2

     [,1] [,2]
[1,]    7   10
[2,]    8   11
[3,]    9   12

> which(X==6, arr.ind=T)
     dim1 dim2 dim3
[1,]    3    2    1
>

HTH
Petr


 
> Uwe Ligges
> 
> > Tim
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html

Petr Pikal
petr.pikal at precheza.cz



More information about the R-help mailing list