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
Hi, On Thu, Apr 22, 2010 at 11:21 AM, Muhammad Rahiz <muhammad.rahiz at ouce.ox.ac.uk> 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) > x0[x2]Two things: 1. Not that it really matters, but why are you using arr.ind=TRUE when x0 is just a "normal" vector? 2. Just skip the middle-man creation of x1: R> x0 <- rnorm(100) R> x2 <- which(x0 > 0 & x0 < 1.5) R> x0[x2] Why not just skip the middle man (x1)? -- Steve Lianoglou Graduate Student: Computational Systems Biology | Memorial Sloan-Kettering Cancer Center | Weill Medical College of Cornell University Contact Info: http://cbio.mskcc.org/~lianos/contact
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
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.