I am trying to use aaply to apply a function to multiple copies (dimension z) of a spatial grid [x,y].
I would like this to behave the same when the x or y dimension is 1 or greater than 1.
So I create an array [x,y,z] and use aaply with .margin=3. When x & y are greater than 1 I have a matrix in the function and I can access the number of rows & columns with nrow & ncol.
The problem I have is that when x or y is 1, the matrix is collapsed to a vector and nrow or ncol give NULL rather than 1 as I would like. The minimal example below illustrates this.
.drop=FALSE doesn't solve because it influences the output rather than the input to the function.
Can anyone suggest a solution ?
Thanks,
Andy
ax2y2 <- array(1, c(2,2,3))
ax1y2 <- array(1, c(1,2,3))
#minimal function to return the final value in a matrix
arTest <- function(m) { m[nrow(m),ncol(m)] }
aaply(ax2y2, .margins=3, .inform=TRUE, function(m) arTest(m) )
aaply(ax1y2, .margins=3, .inform=TRUE, function(m) arTest(m) )
#Error in m[nrow(m), ncol(m)] : incorrect number of dimensions
#Error: with piece 1:
#[1] 1 1