plyr aaply retaining length 1 array dimensions in inputs

16 views
Skip to first unread message

Andy South

unread,
Jun 11, 2014, 6:26:17 AM6/11/14
to manip...@googlegroups.com

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

Martin Zuba

unread,
Aug 31, 2016, 9:59:12 AM8/31/16
to manipulatr
I have exactly the same issue. 
Have you, by chance, found a solution?

Best regards,Martin

Andy South

unread,
Aug 31, 2016, 10:16:43 AM8/31/16
to manipulatr
Alas not. I created a hacky workaround instead.
So if you can find an elegant solution I'll be pleased !
Andy
Reply all
Reply to author
Forward
0 new messages