[R] Using R to illustrate the Central Limit Theorem

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri May 13 01:24:54 CEST 2005


"Kevin E. Thorpe" <kevin.thorpe at utoronto.ca> writes:

> rparab <- function(nn) {
> u <- 2*runif(nn) - 1
> ifelse(u<0,-(abs(u)^(1/3)),u^(1/3))
> }
> 
> 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 help.search("cube root"),
> help.search("root")
> and help.search("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).


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list