controlledApply {arrayMagic} | R Documentation

## controlledApply

### Description

Same functionality as `simpleApply` but possibly faster. In certain circumstances the function `apply` is used instead of `simpleApply` which improves the performace.

### Usage

    controlledApply(arrayObject, dimensions, func, funcResultDimensionality)

### Arguments

`arrayObject` | object of class `array`
`dimensions` | increasing numeric vector
`func` | unary function
`funcResultDimensionality` | numeric (vector) specifying the dimensionality of the return value of `func`

### Value

An array of `dim=c(dim(arrayObject[dimensions]),funcResultDimensionality)`. You may want to use `aperm` to rearrange the dimensions.

### Author(s)

Andreas Buness <a.buness@dkfz.de>

### Examples

    a <- array(c(1:30),dim=c(3,2,5))
    r <- controlledApply(a, 1, function(x){return(x[2,5])}, 1)
    stopifnot( all(r == matrix(data=c(28:30))))

    r <- controlledApply(a, 2, function(x){return(x[,])}, c(3,5))
    stopifnot( all( a == aperm(r,c(2,1,3)) ) )

    vec <- 1:10; dim(vec) <- c(10,1)
    mat <- matrix(data=rep(1:10,4),nrow=10,ncol=4,byrow=FALSE)
    r <- controlledApply(mat,1,function(y){return(mean(y))},1)
    stopifnot(all(r==vec))

    r <- controlledApply(mat, 1:2, function(x) return(x), 1)
    stopifnot( all(r[,,1] == mat) )

    r <- controlledApply(a, c(1,3) , function(x) return(x), dim(a)[2])
    stopifnot( all(aperm(r[,,],c(1,3,2)) == a) )

    r <- controlledApply(a, 1:2, function(x) return(x[2]), 1)
    stopifnot( all(r[,,] == a[,,2]) )

    r <- controlledApply(a, 1, function(x) return(x), c(dim(a)[2],dim(a)[3]))
    stopifnot( all( r== a ) )

[Package Contents]