6

I have a for loop in R in which I want to store the result of each calculation (for all the values looped through). In the for loop a function is called and the output is stored in a variable r in the moment. However, this is overwritten in each successive loop. How could I store the result of each loop through the function and access it afterwards?

Thanks,

example

for (par1 in 1:n) { var<-function(par1,par2) c(var,par1)->var2 print(var2) 

So print returns every instance of var2 but in var2 only the value for the last n is saved..is there any way to get an array of the data or something?

3
  • The way I would do this in .NET would be to use an array and increment the indexer... Commented Nov 22, 2013 at 21:25
  • is there an equivalent in R? Commented Nov 22, 2013 at 21:31
  • Please post your code or an example. The answer would depend on what you're doing. Commented Nov 22, 2013 at 21:45

2 Answers 2

12

initialise an empty object and then assign the value by indexing

a <- 0 for (i in 1:10) { a[i] <- mean(rnorm(50)) } print(a) 

EDIT:

To include an example with two output variables, in the most basic case, create an empty matrix with the number of columns corresponding to your output parameters and the number of rows matching the number of iterations. Then save the output in the matrix, by indexing the row position in your for loop:

n <- 10 mat <- matrix(ncol=2, nrow=n) for (i in 1:n) { var1 <- function_one(i,par1) var2 <- function_two(i,par2) mat[i,] <- c(var1,var2) } print(mat) 

The iteration number i corresponds to the row number in the mat object. So there is no need to explicitly keep track of it.

However, this is just to illustrate the basics. Once you understand the above, it is more efficient to use the elegant solution given by @eddi, especially if you are handling many output variables.

Sign up to request clarification or add additional context in comments.

9 Comments

the problem is my function has several paramters and I would have also to keep track of the exact i..so it shoudl read as output i value i value....
the exact i would correspond to the index of a in which the value from the function is stored. So the ith value of the ith iteration from your loop is a[i]
@TimHeinert This is the correct answer. Try in R and you'll see.
You should never grow an object in a loop, because that will make your code slow. You should pre-allocate it to the correct size (as you do in the second example): a <- numeric(10)
but I have function with two paramters not 2 functions with 1 paramter each so how can I apply this to the question I asked?
|
1

To get a list of results:

n = 3 lapply(1:n, function(par1) { # your function and whatnot, e.g. par1*par1 }) 

Or sapply if you want a vector instead.

A bit more complicated example:

n = 3 some_fn = function(x, y) { x + y } par2 = 4 lapply(1:n, function(par1) { var = some_fn(par1, par2) return(c(var, par1)) # don't have to type return, but I chose to make it explicit here }) #[[1]] #[1] 5 1 # #[[2]] #[1] 6 2 # #[[3]] #[1] 7 3 

2 Comments

where did the for loop go in the respone? and why par1*par1 or is this only example?
@TimHeinert read ?lapply - the apply family of functions is roughly equivalent to for loops; and yeah this is just an example

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.