[R] using which to select range of values

Greg Snow Greg.Snow at imail.org
Thu Apr 22 23:30:35 CEST 2010


Another solution (whether this is better/worse, prettier/uglier, etc./etc. is in the eye of the beholder):

library(TeachingDemos)
x0 <- rnorm(100)
x0[ 0 %<% x0 %<% 1.5 ]

Hope this helps,

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Muhammad Rahiz
> Sent: Thursday, April 22, 2010 9:21 AM
> To: r-help at r-project.org
> Subject: [R] using which to select range of values
> 
> 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)
> x0[x2]
> 
> Thanks.
> 
> --
> 
> Muhammad
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list