[R] Having problems with the ifelse and negative numbers

Kevin Thorpe kev|n@thorpe @end|ng |rom utoronto@c@
Mon Dec 9 14:11:49 CET 2019


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: kevin.thorpe using utoronto.ca  Tel: 416.864.5776  Fax: 416.864.3016
 

On 2019-12-09, 7:58 AM, "R-help on behalf of rsherry8" <r-help-bounces using r-project.org on behalf of rsherry8 using comcast.net> 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
    
    ______________________________________________
    R-help using r-project.org 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.
    



More information about the R-help mailing list