Re: [R] Repeating a function

From: Dennis Murphy <djmuser_at_gmail.com>
Date: Sun, 17 Apr 2011 20:18:45 -0700

Hi:

The r<dist> functions are vectorized, so the loop is unnecessary:

drift <- rbinom(55, 80, 0.4)

or within a function,

drift <- function(p0 = 0.4, N = 40, ngen = 55) rbinom(ngen, 2 * N, p0)

To repeat it 1000 times, you have at least two options:

(1) [1000 columns]
simmat <- replicate(1000, drift())

(2) [1000 rows]
simmat <- matrix(drift(0.4, 40, 1000 * 55), nrow = 1000)

HTH,
Dennis

On Sun, Apr 17, 2011 at 4:48 PM, Sclera <kn0wzal0t_at_gmail.com> wrote:
> Hello all,
>
> I currently have this function:
>
> drift <-function(p0=0.4,N=40,ngen=55){
>        p = p0
>        for( i in 1:ngen){
>                p = rbinom(1,2*N,p)/(2*N)
>                }
>        return( p )
>        }
>
> I want to repeat it 1000 times, then do some analysis on the results. I've
> tried using the rep() function, but that only gives me repeats of the first
> value of p. How can I get 1000 values of p for different iterations of the
> function?
>
> Thanks in advance
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Repeating-a-function-tp3456295p3456295.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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 Mon 18 Apr 2011 - 03:23:47 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 Mon 18 Apr 2011 - 03:50:30 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