s-news
[Top] [All Lists]

Re: Repeating a function

To: paul hughes <u7n96@hotmail.com>
Subject: Re: Repeating a function
From: Matthew Wiener <mcw@ln.nimh.nih.gov>
Date: Tue, 12 Dec 2000 11:58:22 -0500 (EST)
Cc: s-news@lists.biostat.wustl.edu
In-reply-to: <F283jFbUcOV1EetVoMD00014baf@hotmail.com>
Reply-to: Matthew Wiener <mcw@ln.nimh.nih.gov>
Two possibilities.  If your function can be vectorized to run on multiple
arguments at once, that's the way to go.  If not, you can use a loop.

Define a vector (or matrix, or array, if the output
of your function is multidimensional) to hold the repeats, then fill it:

results <- numeric(n)
for(i in 1:n) results[n] <- function(args)
mean(results)

The loop can also be built into your function, if you're going to do this
frequently.  Presumably function() generates some random numbers or is
taking different arguments each time, so it doesn't give the same answer
each time. 

If you've got a matrix or array of results, you can use apply to average
over the correct dimensions.

Hope this helps,

Matt




<Prev in Thread] Current Thread [Next in Thread>