simpleApply {arrayMagic} | R Documentation |
Attention: Be careful with funcResultDimensionality
!
func
is applied to all subsets
of arrayObject
defined by looping over
all indexes given in the dimension dimension
.
func
must be unary.
arrayObject |
array object |
dimension |
numeric |
func |
unary function |
funcResultDimensionality |
numeric (vector) specifying the dimensionality of the result of func |
an array of dim=c(dim(arrayObject)[dimension], funcResultDimensionality); possibly use aperm
to rearrange the dimensions
Andreas Buness <a.buness@dkfz.de>
r <- simpleApply(matrix(c(1, 2)), 1, sum, 1) stopifnot( r == matrix(c(1,2))) a <- array(c(1:30),dim=c(3,2,5)) r <- simpleApply(a, 1, function(x){return(x[1])}, 1) stopifnot( all(r == matrix(data=c(1:3)))) r <- simpleApply(a, 1, function(x){return(x[2,5])}, 1) stopifnot( all(r == matrix(data=c(28:30)))) r <- simpleApply(a, 1, function(x){return(x[1,3])}, 1) stopifnot( all(r == matrix(data=c(13:15)))) r <- simpleApply(a, 1, function(x){return(x[,])}, c(2,5)) stopifnot( all( a == r)) r <- simpleApply(a, 2, function(x){return(x[,])}, c(3,5)) stopifnot( all( a == aperm(r,c(2,1,3)) ) ) r <- simpleApply(a, 3, function(x){return(x[,])}, c(3,2)) stopifnot( all( a == aperm(r,c(2,3,1)) ) ) r2 <- simpleApply(a, 1, function(x){ return(as.vector(x))}, 10) vec <- 1:10 dim(vec) <- c(10,1) mat <- matrix(data=rep(1:10,4),nrow=10,ncol=4,byrow=FALSE) r <- simpleApply(mat,1,function(y){return(mean(y))},1) stopifnot(all(r==vec))