Having problems with the ifelse and negative numbers

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

Having problems with the ifelse and negative numbers

rsherry8
Please consider the following two R statements:
     A =  runif(20, min=-1,max=1)
     ifelse( A < 0, sqrt(-A), A )

The second statement produces the following error message:
     rt(-A) : NaNs produced

I understand that you cannot take the square root of a negative number
but I thought the condition A < 0
would take care of that issue. It appears not to be.

What am I missing?

Thanks,
Bob

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Having problems with the ifelse and negative numbers

Kevin E. Thorpe
The sqrt(-A) is evaluated for all A. The result returned is conditional on the first argument but the other two arguments are evaluated on the entire vector.

Kevin

--
Kevin E. Thorpe
Head of Biostatistics,  Applied Health Research Centre (AHRC)
Li Ka Shing Knowledge Institute of St. Michael's
Assistant Professor, Dalla Lana School of Public Health
University of Toronto
email: [hidden email]  Tel: 416.864.5776  Fax: 416.864.3016
 

On 2019-12-09, 7:58 AM, "R-help on behalf of rsherry8" <[hidden email] on behalf of [hidden email]> wrote:

    Please consider the following two R statements:
         A =  runif(20, min=-1,max=1)
         ifelse( A < 0, sqrt(-A), A )
   
    The second statement produces the following error message:
         rt(-A) : NaNs produced
   
    I understand that you cannot take the square root of a negative number
    but I thought the condition A < 0
    would take care of that issue. It appears not to be.
   
    What am I missing?
   
    Thanks,
    Bob
   
    ______________________________________________
    [hidden email] mailing list -- To UNSUBSCRIBE and more, see
    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.
   

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Having problems with the ifelse and negative numbers

Eric Berger
Hi Bob,
You wrote "the following error message" -
when in fact it is a Warning and not an error message. I think your
code does what you hoped it would do, in the sense it successfully
calculates the sqrt(abs(negativeNumber)), where appropriate.

If you want to run the code without seeing this warning message you can run

ifelse( A < 0, suppressWarnings(sqrt(-A)), A )

and you should be fine.

HTH,
Eric

On Mon, Dec 9, 2019 at 3:18 PM Kevin Thorpe <[hidden email]> wrote:

>
> The sqrt(-A) is evaluated for all A. The result returned is conditional on the first argument but the other two arguments are evaluated on the entire vector.
>
> Kevin
>
> --
> Kevin E. Thorpe
> Head of Biostatistics,  Applied Health Research Centre (AHRC)
> Li Ka Shing Knowledge Institute of St. Michael's
> Assistant Professor, Dalla Lana School of Public Health
> University of Toronto
> email: [hidden email]  Tel: 416.864.5776  Fax: 416.864.3016
>
>
> On 2019-12-09, 7:58 AM, "R-help on behalf of rsherry8" <[hidden email] on behalf of [hidden email]> wrote:
>
>     Please consider the following two R statements:
>          A =  runif(20, min=-1,max=1)
>          ifelse( A < 0, sqrt(-A), A )
>
>     The second statement produces the following error message:
>          rt(-A) : NaNs produced
>
>     I understand that you cannot take the square root of a negative number
>     but I thought the condition A < 0
>     would take care of that issue. It appears not to be.
>
>     What am I missing?
>
>     Thanks,
>     Bob
>
>     ______________________________________________
>     [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>     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.
>
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Having problems with the ifelse and negative numbers

Richard M. Heiberger
or even simpler
sqrt(abs(A))

On Mon, Dec 9, 2019 at 8:45 AM Eric Berger <[hidden email]> wrote:

>
> Hi Bob,
> You wrote "the following error message" -
> when in fact it is a Warning and not an error message. I think your
> code does what you hoped it would do, in the sense it successfully
> calculates the sqrt(abs(negativeNumber)), where appropriate.
>
> If you want to run the code without seeing this warning message you can run
>
> ifelse( A < 0, suppressWarnings(sqrt(-A)), A )
>
> and you should be fine.
>
> HTH,
> Eric
>
> On Mon, Dec 9, 2019 at 3:18 PM Kevin Thorpe <[hidden email]> wrote:
> >
> > The sqrt(-A) is evaluated for all A. The result returned is conditional on the first argument but the other two arguments are evaluated on the entire vector.
> >
> > Kevin
> >
> > --
> > Kevin E. Thorpe
> > Head of Biostatistics,  Applied Health Research Centre (AHRC)
> > Li Ka Shing Knowledge Institute of St. Michael's
> > Assistant Professor, Dalla Lana School of Public Health
> > University of Toronto
> > email: [hidden email]  Tel: 416.864.5776  Fax: 416.864.3016
> >
> >
> > On 2019-12-09, 7:58 AM, "R-help on behalf of rsherry8" <[hidden email] on behalf of [hidden email]> wrote:
> >
> >     Please consider the following two R statements:
> >          A =  runif(20, min=-1,max=1)
> >          ifelse( A < 0, sqrt(-A), A )
> >
> >     The second statement produces the following error message:
> >          rt(-A) : NaNs produced
> >
> >     I understand that you cannot take the square root of a negative number
> >     but I thought the condition A < 0
> >     would take care of that issue. It appears not to be.
> >
> >     What am I missing?
> >
> >     Thanks,
> >     Bob
> >
> >     ______________________________________________
> >     [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> >     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.
> >
> >
> > ______________________________________________
> > [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> > 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.
>
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.
Reply | Threaded
Open this post in threaded view
|

Re: Having problems with the ifelse and negative numbers

R help mailing list-2
Here is a test of the different proposed solutions and a new one faster.
In conclusion, ifelse much be used with caution:

Aini =  runif(1000000, min=-1,max=1)
library(microbenchmark)
A <- Aini
microbenchmark({B1 <- ifelse( A < 0, sqrt(-A), A )})
# mean = 77.55551
A <- Aini
microbenchmark({B2 <- ifelse( A < 0, suppressWarnings(sqrt(-A)), A )})
# mean = 76.53762
A <- Aini
microbenchmark({B3 <- ifelse( A < 0, sqrt(abs(A)), A )})
# mean = 75.26712
A <- Aini
microbenchmark({A[A < 0] <- sqrt(-A[A < 0]);B4 <- A})
# mean = 17.71883

Marc

Le 09/12/2019 à 19:25, Richard M. Heiberger a écrit :

> or even simpler
> sqrt(abs(A))
>
> On Mon, Dec 9, 2019 at 8:45 AM Eric Berger <[hidden email]> wrote:
>> Hi Bob,
>> You wrote "the following error message" -
>> when in fact it is a Warning and not an error message. I think your
>> code does what you hoped it would do, in the sense it successfully
>> calculates the sqrt(abs(negativeNumber)), where appropriate.
>>
>> If you want to run the code without seeing this warning message you can run
>>
>> ifelse( A < 0, suppressWarnings(sqrt(-A)), A )
>>
>> and you should be fine.
>>
>> HTH,
>> Eric
>>
>> On Mon, Dec 9, 2019 at 3:18 PM Kevin Thorpe <[hidden email]> wrote:
>>> The sqrt(-A) is evaluated for all A. The result returned is conditional on the first argument but the other two arguments are evaluated on the entire vector.
>>>
>>> Kevin
>>>
>>> --
>>> Kevin E. Thorpe
>>> Head of Biostatistics,  Applied Health Research Centre (AHRC)
>>> Li Ka Shing Knowledge Institute of St. Michael's
>>> Assistant Professor, Dalla Lana School of Public Health
>>> University of Toronto
>>> email: [hidden email]  Tel: 416.864.5776  Fax: 416.864.3016
>>>
>>>
>>> On 2019-12-09, 7:58 AM, "R-help on behalf of rsherry8" <[hidden email] on behalf of [hidden email]> wrote:
>>>
>>>      Please consider the following two R statements:
>>>           A =  runif(20, min=-1,max=1)
>>>           ifelse( A < 0, sqrt(-A), A )
>>>
>>>      The second statement produces the following error message:
>>>           rt(-A) : NaNs produced
>>>
>>>      I understand that you cannot take the square root of a negative number
>>>      but I thought the condition A < 0
>>>      would take care of that issue. It appears not to be.
>>>
>>>      What am I missing?
>>>
>>>      Thanks,
>>>      Bob
>>>
>>>      ______________________________________________
>>>      [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>>      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.
>>>
>>>
>>> ______________________________________________
>>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>>> 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.
>> ______________________________________________
>> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
>> 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.
> ______________________________________________
> [hidden email] mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
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.