# [R] Simplify the loop over the 3rd dimension of a 3D array

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Thu Jul 12 18:40:42 CEST 2018

```On 12/07/2018 11:34 AM, Marine Regis wrote:
> Hello all,
>
>
> Is there an efficient way to simplify the loop over the 3rd dimension of a 3D array ? I want to keep the loop over the "time". Here is the code:
>
>
> set.seed(12345)
> ind <- 10
> time_seq <- seq(0, 8, 1)
> col_array <- c(paste("time_", time_seq, sep=""))
> tab <- array(0, dim=c(length(time_seq) , length(col_array), ind), dimnames=list(NULL, col_array, as.character(seq(1, ind, 1))))
> print(tab)
>
> tab[1,c("time_0"),] <- round(runif(ind, 0, 100))
> print(tab)
>
>
> for(time in 1:(length(time_seq) - 1)){
>    for(i in 1:ind){
>      tab[time + 1,c("time_0"),i] <- round(runif(1, 0, 100))
>      tab[time + 1,c("time_1"),i] <- tab[time,c("time_0"),i]
>      tab[time + 1,c("time_2"),i] <- tab[time,c("time_1"),i]
>      tab[time + 1,c("time_3"),i] <- tab[time,c("time_2"),i]
>      tab[time + 1,c("time_4"),i] <- tab[time,c("time_3"),i]
>      tab[time + 1,c("time_5"),i] <- tab[time,c("time_4"),i]
>      tab[time + 1,c("time_6"),i] <- tab[time,c("time_5"),i]
>      tab[time + 1,c("time_7"),i] <- tab[time,c("time_6"),i]
>      tab[time + 1,c("time_8"),i] <- tab[time,c("time_7"),i]
>    }
> }

It looks as though you are setting all entries to the same value.  A
simpler way to do that would be this loop:

for(time in 1:(length(time_seq) - 1)){
for(i in 1:ind){
tab[time + 1,,i] <- round(runif(1, 0, 100))
}
}

You could also do away with the inner loop by generating ind random
values all at once.  You have to be a little careful with the ordering;
I think this gets it right:

for(time in 1:(length(time_seq) - 1)){
tab[time + 1,,] <- t(matrix(round(runif(ind, 0, 100)), ind, 9))
}

And then you can do away with the loop entirely, since none of the
values depend on earlier calculations.  Just generate
ind*length(time_seq) uniforms, and put them in the array in the right
order.  You could use aperm() to do this instead of t(), but be careful,
it's easy to get the permutation wrong.  (I'm not even going to try now.
:-).

Duncan Murdoch

>
> print(tab)
>
>
>
> In fact, the array has 800000 observations for the 3rd dimension.
>
>
> Many thanks for your time
>
> Have a great day
>
> Marine
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help