An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121201/bc5ebeff/attachment.pl>
screen if a value is within range
4 messages · Andras Farkas, R. Michael Weylandt, Robert Baer +1 more
15 < a & a < 20
Wrap it in a function if you like:
in_interval <- function(x, interval){
stopifnot(length(interval) == 2L)
interval[1] < x & x < interval[2]
}
in_interval(a, c(15, 20))
Cheers,
Michael
On Sat, Dec 1, 2012 at 4:50 PM, Andras Farkas <motyocska at yahoo.com> wrote:
could you please give me some pointers on how I could make R screen for a value if it falls within a certain range? I looked at the subset function, but is not doing it, perhaps because I only have 1 value to screen? aptreciate the input ex: a <-16.5 I would like to screen to see if a is within the range of 15 to 20, (which it is:-)), and I would like the code to return a value of 1 if a is within the specified range or 0 if it is not.
On 12/1/2012 10:50 AM, Andras Farkas wrote:
Dear all, could you please give me some pointers on how I could make R screen for a value if it falls within a certain range? I looked at the subset function, but is not doing it, perhaps because I only have 1 value to screen? aptreciate the input ex: a <-16.5 I would like to screen to see if a is within the range of 15 to 20, (which it is:-)), and I would like the code to return a value of 1 if a is within the specified range or 0 if it is not. Apreciate the insights, Andras [[alternative HTML version deleted]]
a = 16.5 ans = (a >= 15 & a <= 20) ans [1] TRUE Rob
On Dec 1, 2012, at 9:55 AM, R. Michael Weylandt wrote:
15 < a & a < 20
Wrap it in a function if you like:
in_interval <- function(x, interval){
stopifnot(length(interval) == 2L)
interval[1] < x & x < interval[2]
}
in_interval(a, c(15, 20))
Could just use the findInterval function: findInterval(x, c(15,20) ) == 1 findInterval returns a numeric vector indicating which bin(s) the argument vector element(s) fall(s) into. Items below the lower bound get a zero which means if the result is used as an index the there will be no item chosen for that value. Items above the maximal boundary get a value of n+1 where n is the number of bins. (Very useful function.)
David. > Cheers, > Michael > > On Sat, Dec 1, 2012 at 4:50 PM, Andras Farkas <motyocska at yahoo.com> > wrote: >> >> could you please give me some pointers on how I could make R screen >> for a value if it falls within a certain range? >> I looked at the subset function, but is not doing it, perhaps >> because I only have 1 value to screen? >> >> aptreciate the input >> >> ex: >> a <-16.5 >> I would like to screen to see if a is within the range of 15 to 20, >> (which it is:-)), and I would like the code to return a value of 1 >> if a is within the specified range or 0 if it is not. >> > > ______________________________________________ > 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. David Winsemius, MD Alameda, CA, USA