figure out whether 1 is between two numbers

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

figure out whether 1 is between two numbers

Taka Matzmoto
Hi R users

I have two variables (X and Y)

X <-  rnorm(100,.7,.5)
Y <-  rnorm (100,.3,.1)

I like to know whether 1 is between each pair of X and Y or not.

Thanks

TM

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: figure out whether 1 is between two numbers

jholtman
(X < 1) & (Y > 1)

On 2/18/06, Taka Matzmoto <[hidden email]> wrote:

>
> Hi R users
>
> I have two variables (X and Y)
>
> X <-  rnorm(100,.7,.5)
> Y <-  rnorm (100,.3,.1)
>
> I like to know whether 1 is between each pair of X and Y or not.
>
> Thanks
>
> TM
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What the problem you are trying to solve?

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: figure out whether 1 is between two numbers

Robert Baer
In reply to this post by Taka Matzmoto
Try:
(X>1 & Y<1) | (X<1 & Y>1)

Rob
----- Original Message -----
From: "Taka Matzmoto" <[hidden email]>
To: <[hidden email]>
Sent: Friday, February 17, 2006 11:20 PM
Subject: [R] figure out whether 1 is between two numbers


> Hi R users
>
> I have two variables (X and Y)
>
> X <-  rnorm(100,.7,.5)
> Y <-  rnorm (100,.3,.1)
>
> I like to know whether 1 is between each pair of X and Y or not.
>
> Thanks
>
> TM
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: figure out whether 1 is between two numbers

Duncan Murdoch
On 2/18/2006 1:13 AM, Robert W. Baer, Ph.D. wrote:
> Try:
> (X>1 & Y<1) | (X<1 & Y>1)

This is a minor stylistic suggestion: when translating the mathematical
expression (a < b < c), write it as (a < b) & (b < c).  It has no effect
on the outcome, but keeping the inequalities in the same direction makes
the meaning more obvious.

If you do this, your expression becomes

(Y<1 & 1<X) | (X<1 & 1<Y)

and it's clear at a glance what's going on.

I think I originally read this suggestion in something written by
Kernighan and/or Plauger, I can't claim credit for it.

Duncan Murdoch

>
> Rob
> ----- Original Message -----
> From: "Taka Matzmoto" <[hidden email]>
> To: <[hidden email]>
> Sent: Friday, February 17, 2006 11:20 PM
> Subject: [R] figure out whether 1 is between two numbers
>
>
>> Hi R users
>>
>> I have two variables (X and Y)
>>
>> X <-  rnorm(100,.7,.5)
>> Y <-  rnorm (100,.3,.1)
>>
>> I like to know whether 1 is between each pair of X and Y or not.
>>
>> Thanks
>>
>> TM
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide!
>> http://www.R-project.org/posting-guide.html
>>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: figure out whether 1 is between two numbers

Ted.Harding
On 18-Feb-06 Duncan Murdoch wrote:

> On 2/18/2006 1:13 AM, Robert W. Baer, Ph.D. wrote:
>> Try:
>> (X>1 & Y<1) | (X<1 & Y>1)
>
> This is a minor stylistic suggestion: when translating the mathematical
> expression (a < b < c), write it as (a < b) & (b < c).  It has no
> effect
> on the outcome, but keeping the inequalities in the same direction
> makes
> the meaning more obvious.
>
> If you do this, your expression becomes
>
> (Y<1 & 1<X) | (X<1 & 1<Y)
>
> and it's clear at a glance what's going on.
>
> I think I originally read this suggestion in something written by
> Kernighan and/or Plauger, I can't claim credit for it.
>
> Duncan Murdoch

Mention of "Kernighan and/or Plauger" got me digging out K & P
"The Elements of Programming Style" (from 1974/78). The suggestion
is indeed in there, on pp 20-21, leading up to the rule "Use
the 'telephone test' for readability" ("If someone could
understand your code when read aloud over the telephone, it's
clear enough. If not, then it needs rewriting.")

The example is:

  Consider the sequence

    6 IF(X1.GE.ARRAY(I)) GO TO 2
      IF(ARRAY(I).LT.X2) ICOUNT=ICOUNT+1
    2 ...

  It takes a while to realize that IVOUNT is incremented only
  if ARRAY(I) lies between X1 and X2. .... Rewriting gives:

    6 IF(ARRAY(I).GT.X1 .AND. ARRAY(I).LT.X2) ICOUNT = ICOUNT + 1

(Though I think it could be improved still further by the slight
change to "X1.LT.ARRAY(I)" to bring it into line with Duncan's
example).

Best wishes,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <[hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 18-Feb-06                                       Time: 14:39:50
------------------------------ XFMail ------------------------------

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Reply | Threaded
Open this post in threaded view
|

Re: figure out whether 1 is between two numbers

Ted.Harding
In reply to this post by Duncan Murdoch
On 18-Feb-06 Duncan Murdoch wrote:

> On 2/18/2006 1:13 AM, Robert W. Baer, Ph.D. wrote:
>> Try:
>> (X>1 & Y<1) | (X<1 & Y>1)
>
> This is a minor stylistic suggestion: when translating the
> mathematical expression (a < b < c), write it as
> (a < b) & (b < c). It has no effect on the outcome, but
> keeping the inequalities in the same direction makes
> the meaning more obvious.
>
> If you do this, your expression becomes
>
> (Y<1 & 1<X) | (X<1 & 1<Y)
>
> and it's clear at a glance what's going on.

And, as a follow-up, can I suggest for ultimate clarity [ :) ]

  (X<1 | Y<1) & (1<X | 1<Y)

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <[hidden email]>
Fax-to-email: +44 (0)870 094 0861
Date: 19-Feb-06                                       Time: 10:16:55
------------------------------ XFMail ------------------------------

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html