[R] Simplify the loop over the 3rd dimension of a 3D array
Marine Regis
m@rine@regi@ @ending from hotm@il@fr
Thu Jul 12 17:34:50 CEST 2018
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]
}
}
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]]
More information about the R-help
mailing list