Re: [R] looping problem

From: Uwe Ligges <ligges_at_statistik.tu-dortmund.de>
Date: Thu, 24 Apr 2008 10:55:28 +0200

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.



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. Received on Thu 24 Apr 2008 - 10:01:59 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 24 Apr 2008 - 11:30:32 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive