Re: [R] Number in interval

2011-07-18 Thread Greg Snow
Good point, I have added a note and example to the documentation to this effect.

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


> -Original Message-
> From: William Dunlap [mailto:wdun...@tibco.com]
> Sent: Monday, July 18, 2011 3:10 PM
> To: Greg Snow; jim holtman; Manuel K.
> Cc: r-help@r-project.org
> Subject: RE: [R] Number in interval
> 
> Note that the precendence of %<=% is not the
> same as that of <=, so you can be surprised by its
> behavior in slightly more complex expressions:
> 
>   > z <- seq(1.2, len=5, by=.7)
>   > which( 2 %<=% z %<=% 3 ) # same as which(2<=z & z<=3)
>   [1] 3
>   > which( 2 %<=% z %<=% 1.5*2 )
>   Error in which(2 %<=% z %<=% 1.5 * 2) :
> argument to 'which' is not logical
> 
> 
> Bill Dunlap
> Spotfire, TIBCO Software
> wdunlap tibco.com
> 
> > -Original Message-
> > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of Greg Snow
> > Sent: Monday, July 18, 2011 1:39 PM
> > To: jim holtman; Manuel K.
> > Cc: r-help@r-project.org
> > Subject: Re: [R] Number in interval
> >
> > A small modification of this would be:
> >
> > > library(TeachingDemos)
> > > -3 %<=% z %<=% 3
> >
> > Whether that is prettier or uglier than Jim's answer is in the eye of
> the beholder (for longer
> > variable names this version could save a few key strokes).
> >
> > --
> > Gregory (Greg) L. Snow Ph.D.
> > Statistical Data Center
> > Intermountain Healthcare
> > greg.s...@imail.org
> > 801.408.8111
> >
> >
> > > -Original Message-
> > > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> > > project.org] On Behalf Of jim holtman
> > > Sent: Sunday, July 17, 2011 3:29 PM
> > > To: Manuel K.
> > > Cc: r-help@r-project.org
> > > Subject: Re: [R] Number in interval
> > >
> > > try this:
> > >
> > > >  z <- c(-1.4,0.5,4.7)
> > > > (z >= -3) & (z <= 3)
> > > [1]  TRUE  TRUE FALSE
> > > >
> > >
> > >
> > > On Sun, Jul 17, 2011 at 10:54 AM, Manuel K. 
> wrote:
> > > > Hi all,
> > > >
> > > > I have an interval (e.g [-3,3]) and a numeric vector z (-
> > > 1.4,0.5,4.7). How
> > > > can I test whether an element in z lies between between -3,3? I
> > > particularly
> > > > need a TRUE/FALSE response.
> > > >
> > > > Thanks
> > > > Manuel
> > > >
> > > > --
> > > > View this message in context:
> http://r.789695.n4.nabble.com/Number-
> > > in-interval-tp3673537p3673537.html
> > > > Sent from the R help mailing list archive at Nabble.com.
> > > >
> > > > __
> > > > R-help@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.
> > > >
> > >
> > >
> > >
> > > --
> > > Jim Holtman
> > > Data Munger Guru
> > >
> > > What is the problem that you are trying to solve?
> > >
> > > __
> > > R-help@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.
> >
> > __
> > R-help@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.

__
R-help@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.


Re: [R] Number in interval

2011-07-18 Thread William Dunlap
Note that the precendence of %<=% is not the
same as that of <=, so you can be surprised by its
behavior in slightly more complex expressions:

  > z <- seq(1.2, len=5, by=.7)
  > which( 2 %<=% z %<=% 3 ) # same as which(2<=z & z<=3)
  [1] 3
  > which( 2 %<=% z %<=% 1.5*2 )
  Error in which(2 %<=% z %<=% 1.5 * 2) : 
argument to 'which' is not logical


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On 
> Behalf Of Greg Snow
> Sent: Monday, July 18, 2011 1:39 PM
> To: jim holtman; Manuel K.
> Cc: r-help@r-project.org
> Subject: Re: [R] Number in interval
> 
> A small modification of this would be:
> 
> > library(TeachingDemos)
> > -3 %<=% z %<=% 3
> 
> Whether that is prettier or uglier than Jim's answer is in the eye of the 
> beholder (for longer
> variable names this version could save a few key strokes).
> 
> --
> Gregory (Greg) L. Snow Ph.D.
> Statistical Data Center
> Intermountain Healthcare
> greg.s...@imail.org
> 801.408.8111
> 
> 
> > -Original Message-
> > From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> > project.org] On Behalf Of jim holtman
> > Sent: Sunday, July 17, 2011 3:29 PM
> > To: Manuel K.
> > Cc: r-help@r-project.org
> > Subject: Re: [R] Number in interval
> >
> > try this:
> >
> > >  z <- c(-1.4,0.5,4.7)
> > > (z >= -3) & (z <= 3)
> > [1]  TRUE  TRUE FALSE
> > >
> >
> >
> > On Sun, Jul 17, 2011 at 10:54 AM, Manuel K.  wrote:
> > > Hi all,
> > >
> > > I have an interval (e.g [-3,3]) and a numeric vector z (-
> > 1.4,0.5,4.7). How
> > > can I test whether an element in z lies between between -3,3? I
> > particularly
> > > need a TRUE/FALSE response.
> > >
> > > Thanks
> > > Manuel
> > >
> > > --
> > > View this message in context: http://r.789695.n4.nabble.com/Number-
> > in-interval-tp3673537p3673537.html
> > > Sent from the R help mailing list archive at Nabble.com.
> > >
> > > __
> > > R-help@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.
> > >
> >
> >
> >
> > --
> > Jim Holtman
> > Data Munger Guru
> >
> > What is the problem that you are trying to solve?
> >
> > __
> > R-help@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.
> 
> __
> R-help@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.

__
R-help@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.


Re: [R] Number in interval

2011-07-18 Thread Greg Snow
A small modification of this would be:

> library(TeachingDemos)
> -3 %<=% z %<=% 3

Whether that is prettier or uglier than Jim's answer is in the eye of the 
beholder (for longer variable names this version could save a few key strokes).

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


> -Original Message-
> From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
> project.org] On Behalf Of jim holtman
> Sent: Sunday, July 17, 2011 3:29 PM
> To: Manuel K.
> Cc: r-help@r-project.org
> Subject: Re: [R] Number in interval
> 
> try this:
> 
> >  z <- c(-1.4,0.5,4.7)
> > (z >= -3) & (z <= 3)
> [1]  TRUE  TRUE FALSE
> >
> 
> 
> On Sun, Jul 17, 2011 at 10:54 AM, Manuel K.  wrote:
> > Hi all,
> >
> > I have an interval (e.g [-3,3]) and a numeric vector z (-
> 1.4,0.5,4.7). How
> > can I test whether an element in z lies between between -3,3? I
> particularly
> > need a TRUE/FALSE response.
> >
> > Thanks
> > Manuel
> >
> > --
> > View this message in context: http://r.789695.n4.nabble.com/Number-
> in-interval-tp3673537p3673537.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > __
> > R-help@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.
> >
> 
> 
> 
> --
> Jim Holtman
> Data Munger Guru
> 
> What is the problem that you are trying to solve?
> 
> __
> R-help@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.

__
R-help@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.


Re: [R] Number in interval

2011-07-18 Thread Manuel K.
Thank you Jim, Steve and David. 

I found findIntervals to work best for my problem.

Manuel

--
View this message in context: 
http://r.789695.n4.nabble.com/Number-in-interval-tp3673537p3674969.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Number in interval

2011-07-17 Thread David Winsemius


On Jul 17, 2011, at 5:29 PM, jim holtman wrote:


try this:


z <- c(-1.4,0.5,4.7)
(z >= -3) & (z <= 3)

[1]  TRUE  TRUE FALSE





Another way:

> findInterval(z, c(-3,3)) == 1
[1]  TRUE  TRUE FALSE
> z=c(-50,-1.4,0.5,4.7)

And just to prove to myself that it behaves as I expect with values  
below -3:


> findInterval(z, c(-3,3)) == 1
[1] FALSE  TRUE  TRUE FALSE

You may need to check that the method you choose behaves as you expect  
at the extremes. findInterval behaves the same way as cut() does in  
its default mode. The maximum value matches but the minimum value does  
not:


> z=c(-50,-1.4,0.5,4.7, 3, -3)
> findInterval(z, c(-3,3)) == 1
[1] FALSE  TRUE  TRUE FALSE FALSE  TRUE

> cut(seq(-4, 4, by=1), breaks=c(-3,3))
[1]   (-3,3] (-3,3] (-3,3] (-3,3] (-3,3] (-3,3] 
Levels: (-3,3]

Jim's method lets you determine if you want inclusivity at both or  
either end.

--
David.



On Sun, Jul 17, 2011 at 10:54 AM, Manuel K.   
wrote:

Hi all,

I have an interval (e.g [-3,3]) and a numeric vector z  
(-1.4,0.5,4.7). How
can I test whether an element in z lies between between -3,3? I  
particularly

need a TRUE/FALSE response.

Thanks
Manuel

--
View this message in context: 
http://r.789695.n4.nabble.com/Number-in-interval-tp3673537p3673537.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.





--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

__
R-help@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
West Hartford, CT

__
R-help@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.


Re: [R] Number in interval

2011-07-17 Thread Steve Lianoglou
Also check out the 'cut' and 'findIntervals' functions if you need to
check for many intervals at once.

On Sunday, July 17, 2011, jim holtman  wrote:
> try this:
>
>>  z <- c(-1.4,0.5,4.7)
>> (z >= -3) & (z <= 3)
> [1]  TRUE  TRUE FALSE
>>
>
>
> On Sun, Jul 17, 2011 at 10:54 AM, Manuel K.  wrote:
>> Hi all,
>>
>> I have an interval (e.g [-3,3]) and a numeric vector z (-1.4,0.5,4.7). How
>> can I test whether an element in z lies between between -3,3? I particularly
>> need a TRUE/FALSE response.
>>
>> Thanks
>> Manuel
>>
>> --
>> View this message in context: 
>> http://r.789695.n4.nabble.com/Number-in-interval-tp3673537p3673537.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> __
>> R-help@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.
>>
>
>
>
> --
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
>
> __
> R-help@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.
>

-- 
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

__
R-help@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.


Re: [R] Number in interval

2011-07-17 Thread jim holtman
try this:

>  z <- c(-1.4,0.5,4.7)
> (z >= -3) & (z <= 3)
[1]  TRUE  TRUE FALSE
>


On Sun, Jul 17, 2011 at 10:54 AM, Manuel K.  wrote:
> Hi all,
>
> I have an interval (e.g [-3,3]) and a numeric vector z (-1.4,0.5,4.7). How
> can I test whether an element in z lies between between -3,3? I particularly
> need a TRUE/FALSE response.
>
> Thanks
> Manuel
>
> --
> View this message in context: 
> http://r.789695.n4.nabble.com/Number-in-interval-tp3673537p3673537.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@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.
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

__
R-help@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.


[R] Number in interval

2011-07-17 Thread Manuel K.
Hi all, 

I have an interval (e.g [-3,3]) and a numeric vector z (-1.4,0.5,4.7). How
can I test whether an element in z lies between between -3,3? I particularly
need a TRUE/FALSE response.

Thanks
Manuel  

--
View this message in context: 
http://r.789695.n4.nabble.com/Number-in-interval-tp3673537p3673537.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.