[R] multivariate way to aaply on different arrays

Jannis bt_jannis at yahoo.de
Tue Oct 23 15:29:18 CEST 2012


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:
> Dear R community,
>
>
> is there any efficient way to use aaply on different datacubes? I have 3
> dimesniolan datacubes/arrays with dimensions lon x lat x time. Now I
> would like to do caclulations on each individual time series (e.g. all
> vectors along the third dimension) using a time series (or more) in the
> same location on another (identically shaped) array.
>
> What would be the most efficient way to do this? Is there any
> multivariate version of aaply? Or other functions? Or some memory
> efficient and fast way (the datacubes are huge!) to do this by hand?
>
>
> Thanks a lot
>
> Jannis
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list