Skip to content
Prev 258908 / 398502 Next

Saving Values in a Vector from a For Loop

Hi,
On Wed, May 4, 2011 at 10:19 AM, blutack <x-jess-h-x at hotmail.co.uk> wrote:
R> n.times <- 100
R> result <- numeric(n.times) ## assuming your function returns numeric
R> for (i in 1:n.times) {
  result[i] <- myfunction(...)
}

or

R> result <- replicate(n.times, myfunction(...))

or if you need the index

R> result <- sapply(seq(n.times), function(i) myfunction(i, ...))

I guess you get the idea ...