simpleApply            package:arrayMagic            R Documentation

_s_i_m_p_l_e_A_p_p_l_y

_D_e_s_c_r_i_p_t_i_o_n:

     Note: very slow. 'func' is applied to all subsets of 'arrayObject'
     defined by 'dimensions', i.e. for every element i of
     'arrayObject'['dimensions'] the function 'func' is applied to
     'arrayObject'[i]. 'func' must be unary. Due to the recursive
     definition the function might not only be slow but also very
     memory intensive. The function aims to give you more control on
     the dimensionality of the return value in contrast to 'apply'; cf.
     the examples. Attention: Be careful with
     'funcResultDimensionality' !

_U_s_a_g_e:

     simpleApply(arrayObject, dimensions, func, funcResultDimensionality, DEBUG=FALSE)

_A_r_g_u_m_e_n_t_s:

arrayObject: object of type 'array'; required; default: missing

dimensions: increasing numeric vector; required; default: missing

    func: unary function; required; default: missing

funcResultDimensionality: numeric (vector) specifying the
          dimensionality of the result value of 'func'

   DEBUG: logical; required; default: 'FALSE'; to trace the recursive
          calling you may use 'DEBUG=TRUE'

_V_a_l_u_e:

     'array' of 'dim=c(dim( arrayObject[dimensions] ),
     funcResultDimensionality     )'; possibly use 'aperm' to rearrange
     the dimensions

_A_u_t_h_o_r(_s):

     Andreas Buness <a.buness@dkfz.de>

_E_x_a_m_p_l_e_s:

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

     r <- simpleApply(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 <- simpleApply(mat,1,function(y){return(mean(y))},1)
     stopifnot(all(r==vec))

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

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

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

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

      

             

