[R] looping problem

Uwe Ligges ligges at statistik.tu-dortmund.de
Thu Apr 24 10:55:28 CEST 2008



Roslina Zakaria wrote:
> Dear r-expert,
> I would like to generate 30 sets of random numbers from uniform distribution (0,1). Each set of random numbers should have 90 data.  Here is my code:
> rand.no <- function(n,itr)
> { for (i in 1:itr)
>   {rand.1 <- runif(n,0,1)
>   if (i ==1) rand.2 <- rand.1
>   else rand.2 <- cbind(rand.2,rand.1)
>   }
>   rand.2
> }
> Question: The code is okay is just that it give me 31 sets instead of 30 sets. 

Works for me as expected, but really, why don't you simply say:

rand.no <- function(n,itr){
     matrix(runif(n*itr, 0, 1), nrow=n, ncol=itr)
}

which does the same, just much quicker...

Uwe Ligges




> Can anybody any adjustment to my code.
> Your help is really appreciated.
> Thank you.
> 
> 
>       ____________________________________________________________________________________
> Be a better friend, newshound, and 
> know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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