Re: [R] Having problems with the ifelse and negative numbers

2019-12-10 Thread Marc Girondot via R-help
Here is a test of the different proposed solutions and a new one faster. In conclusion, ifelse much be used with caution: Aini =  runif(100, min=-1,max=1) library(microbenchmark) A <- Aini microbenchmark({B1 <- ifelse( A < 0, sqrt(-A), A )}) # mean = 77.1 A <- Aini microbenchmark({B2 <-

Re: [R] Having problems with the ifelse and negative numbers

2019-12-09 Thread Richard M. Heiberger
or even simpler sqrt(abs(A)) On Mon, Dec 9, 2019 at 8:45 AM Eric Berger 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

Re: [R] Having problems with the ifelse and negative numbers

2019-12-09 Thread 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

Re: [R] Having problems with the ifelse and negative numbers

2019-12-09 Thread Kevin 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

[R] Having problems with the ifelse and negative numbers

2019-12-09 Thread 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