[R] Slicing an array

Jim Lemon jim at bitwrit.com.au
Fri Jun 12 14:36:24 CEST 2009


Hi all,
I've been trying to work out how to slice an array of arbitrary 
dimension. I found a message showing how to stick two arrays together, 
but so far have been unable to transform that into what I need. Assume 
that I have a four dimensional array for example:

array4d
, , 1, 1

     [,1] [,2]
[1,]    3    6
[2,]    1    1

, , 2, 1

     [,1] [,2]
[1,]    4    7
[2,]    8    7

, , 1, 2

     [,1] [,2]
[1,]    7    8
[2,]    8    7

, , 2, 2

     [,1] [,2]
[1,]    7    5
[2,]    9    9

I want to slice the array from the rightmost dimension, getting in sequence:

array4d[,,,1]
array4d[,,,2]

then slicing those 3D arrays, and so on. I'm pretty sure that this will 
involve a:

do.call('[',...)

construction, but I haven't been able to work out how to specify the 
list of arguments, in particular the index vector, to slice up the last 
dimension. I have tried constructing index vectors with NULLs which of 
course doesn't work because they just disappear. Any suggestions?

Jim




More information about the R-help mailing list