[R] Repeating a function

Dennis Murphy djmuser at gmail.com
Mon Apr 18 05:18:45 CEST 2011


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.
>



More information about the R-help mailing list