Skip to content
Prev 308761 / 398503 Next

multivariate way to aaply on different arrays

Hi R gurus,


just in case anybody else has a similar problem ... I have programmed a 
function that solves this problem by creating a higher dimensional array 
out of the individual variables. I have no idea though whether this is 
very efficient. Feel welcome to comment in case you think that there are 
more efficient ways to solve this.

Best
Jannis


maaply <- structure(function(
##title<< multivariate version of aaply
##description<< apply a function to slices, vectors in different 
(multivariate)
##              multidimensional datacubes.
    ...          ##<< arrays: input arrays
    , margins    ##<< margins/dimensions along which to split the arrays 
(see help of apply).
    , fun        ##<< function to apply to each cutout from the arrays. 
  (see help of apply)
                 ##   Has to have only ... as an input.
    , parallel = TRUE ##<< logical: whether to parallelize the 
calculation (see help of aaply)
    )
##seealso<<
##\code{\link{apply}}, \code{\link{aaply}}
{
   require(abind)
   require(plyr)
   data <- abind(..., along =.5)
   fun.internal <- function(data) {
     results <- do.call(fun, alply(data, 1, function(x)return(x)))
     return(results)
   }
   return(aaply(.data = data, .margins = c(margins + 1), .fun = 
fun.internal, .parallel = parallel))
}, ex = function(){
   #example datacubes
   data1 <- aperm((array(rep(1:100, each = 10), dim = c(10,10,10))), 
c(2,3,1))
   data2 <- array(rep(2, 10^3), c(10,10,10))
   data3 <- data1

   #example function
   fun = function(...) {
     dummy <- list(...)
     return(sum(dummy[[1]]) / mean(dummy[[2]]) + mean(dummy[[3]]))
   }

   #call to function
   results <- maaply(data1,data2,data3, margins = c(1,2), fun = fun)
})
On 18.10.2012 17:13, Jannis wrote: