[R] using which to select range of values

David Winsemius dwinsemius at comcast.net
Thu Apr 22 17:58:10 CEST 2010


On Apr 22, 2010, at 11:21 AM, Muhammad Rahiz wrote:

> Hi all,
>
> I would like to get the array index for a range of values, say  0 <  
> x < 1.5. I'm wondering if there is an alternative for the following  
> which I've done
>
> x0 <- rnorm(100)
> x1 <- ifelse(x0 > 0 & x0 < 1.5,"t","f")
> x2 <- which(x1=="t",arr.ind=TRUE)

A further point. Since "x1" was not an array, setting arr.ind=TRUE did  
nothing. That flag is designed to allow return of multiple dimensional  
results:

 > arr <- array(1:27, c(3,3,3) )
 > which(arr==9)
[1] 9
 > which(arr==9, arr.ind=TRUE)
      dim1 dim2 dim3
[1,]    3    3    1


> x0[x2]
>
> Thanks.
>
> -- 
-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list