0

I have a function which is executed 200 times like this

for (l in 1:200) {
fun.ction(paramter1=g, paramter2=h)$element->u[z,,]
}

u is an array:

u<-array(NA, dim=c(2000,150,7))

of which I know it should have the right format. The element of func.tion is also an array which has the same dimensions. Hence, is there some way to fill the array u in each of the 200 runs with the array resulting from fun.ction()$element? I tried to use indexing via a list (u[[z]]). It saves the array but as a list so that I can't access the elements afterwards which I would need to. I appreciate any help.

Thomas
  • 43,637
  • 12
  • 109
  • 140
user3069326
  • 575
  • 1
  • 7
  • 10
  • What's `z`? The variable you use to index the for-loop (`l`) should probably be the same index you use to populate the `u` object. – Thomas Dec 18 '13 at 12:06
  • 4
    I guess you should use `sapply` instead of a `for` loop, but your code is not [reproducible](http://stackoverflow.com/a/5963610/1412059). So we can't really help you. – Roland Dec 18 '13 at 12:32

1 Answers1

0

I am not sure what it is that you want, but if you just want to store 200 arrays of dimensions (2000,150,7) you can just make another array with a fourth dimension of 200.

storage.array <- array(dim=c(2000,150,7, 200))

And then store your (2000, 150, 7) arrays in the fourth dimension:

for (i in 1:200){ 
                storage.array[,,,i] <- 
                                     fun.ction(paramter1=g, paramter2=h)$element}

Then you can access each of the ith array by:

storage.array[,,,i]

But I guess that will be too big an array for R to handle, at least it is in my computer.

An example that you can easily reproduce with smaller arrays:

storage.array <- array(dim=c(20,2,7, 200))

fun.ction <- function(parameter1, parameter2){
  array(rnorm(140, parameter1, parameter2), dim=c(20,2,7))
}

for (i in 1:200){
  storage.array[,,,i]<- fun.ction(10, 10)
}

But as Roland and Thomas have said, you should make your code reproducible and define correctly what you want, so it is easier to answer without trying to guess what your problem is.

Best regards

Carlos Cinelli
  • 11,354
  • 9
  • 43
  • 66