I am working with an array with dimensions
[1] 290 259 55 4
For each repetition of the last three dimensions, I want to perform a rolling mean on the 290 elements of the first dimension, reducing the number of elements to 289. Finally, I need to create a data frame with the updated values.
The following code achieves what I need, but it takes a LONG time to run (actually, I have to interrupt it before the end).
library(zoo)
# Generate random data with same dimensions as mine
my.array <- array(1:16524200, dim=c(290,259,55,4))
# Get dimension sizes
dim2 <- dim(my.array)[2]
dim3 <- dim(my.array)[3]
dim4 <- dim(my.array)[4]
# Pre-allocate data frame to be used within the loop
df2 <- data.frame()
# Loop over dimensions
for (i in 1:dim4) {
for (j in 1:dim3) {
for (k in 1:dim2) {
# Take rolling average
u <- rollapply(my.array[,k,j,i], 2, mean)
# Assemble data frame
df1 <- data.frame(time=i, level=j, lat=k, wind=u)
df2 <- rbind(df2, df1)
}
}
}
# Very slow, and uses only one machine core
I feel like it is possible to improve the processing time of this code by using vectorization or even some kind of parallelism, but I can't figure out how.
Any suggestions to make this code more efficient?