Re: [R] Using R to illustrate the Central Limit Theorem

From: Kevin E. Thorpe <>
Date: Fri 13 May 2005 - 22:45:28 EST

Thanks. I'd forgotton that detail about ifelse. Also, I was so focused on the U[0,1] it totally did not occur to me to go direct from U[-1,1]. Your suggestion should obviate the need for a local function at all.

Peter Dalgaard wrote:

>"Kevin E. Thorpe" <> writes:
>>rparab <- function(nn) {
>>u <- 2*runif(nn) - 1
>>It seems that in my version of R (2.0.1) on Linux, that calculating the cube
>>root of a negative number using ^(1/3) returns NaN. I looked at the help in
>>the arithmetic operators and did"cube root"),
>>and"cube") and recognised no alternatives. So I used an
>>ifelse() to
>>deal with the negatives. Have I missed something really elementary?
>Not really. You might have used u <- runif(nn,-1,1) and
>sign(u)*abs(u)^(1/3) instead of the ifelse construct (remember that
>ifelse generally evaluates both the 'yes' and 'no' parts and on some
>architectures the NaN results may be slow to compute).

Kevin E. Thorpe
Biostatistician/Trialist, Knowledge Translation Program
Assistant Professor, Department of Public Health Sciences
Faculty of Medicine, University of Toronto
email:  Tel: 416.946.8081  Fax: 416.971.2462

______________________________________________ mailing list
PLEASE do read the posting guide!
Received on Fri May 13 22:49:32 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:31:45 EST