Skip to content
Prev 172048 / 398506 Next

Large 3d array manipulation

On Feb 27, 2009, at 7:49 PM, Duncan Murdoch wrote:

            
That's rather interesting. I had not realized that one could use that  
construction with apply. I had earlier tried to substitute rollmean  
for the inner loop. For one thing I thought that trying to index 768+7  
was going to create some problems with out-of-range indexing. I got  
the same error with my effort to insert rollmean in the OP's  
construction as I do in this construction:

myVar <- array(1:(243*246*768), dim=c(243,246,768))
 > myVar[1,1,1:8]
[1]      1  59779 119557 179335 239113 298891 358669 418447

newVar <-array(,dim=c(243,246,768))
library(zoo)
for (k in 1:768)  newVar[,,k] <- apply(myVar, 1:2, function(x)  
mean(x[k:(k+7)]))
Error in newVar[, , k] <- apply(myVar, 1:2, function(x) rollmean(x,  
8)) :
   number of items to replace is not a multiple of replacement length

I am guessing that at some point the assignment function cannot  
resolve which index in newVar to use. So I tried redimensioning newVar  
to only have 761 as it third dimension and using:

  myVar[1,1,1:8]

for (k in 1:761)  {newVar[ , , ] <- apply(myVar, 1:2, function(x)  
mean(x[k:(k+7)])) ; print(k)}

This may be working. This executes in less than 20 seconds:

 > newVar <-array(,dim=c(243,246,761))
 > str(newVar); Sys.time()
  logi [1:243, 1:246, 1:761] NA NA NA NA NA NA ...
[1] "2009-02-27 22:35:48 EST"
 >  newVar[,,] <- apply(myVar, 1:2, function(x) rollmean(x, 8));  
Sys.time()
[1] "2009-02-27 22:35:57 EST"
 > str(newVar)
  num [1:243, 1:246, 1:761] 209224 269002 328780 388558 448336 ...
 >