Sean Zhang
2011-Jan-17 20:20 UTC
[R] how to cut a multidimensional array along a chosen dimension and store each piece into a list
Dear R-Helpers, I wonder whether there is a function which cuts a multiple dimensional array along a chosen dimension and then store each piece (still an array of one dimension less) into a list. For example, arr <- array(seq(1*2*3*4),dim=c(1,2,3,4)) # I made a point to set the length of the first dimension be 1to test whether I worry about drop=F option. brkArrIntoListAlong <- function(arr,alongWhichDim){ #### return(outlist) } I have tried splitter_a in plyr package but does not get what I want. library(plyr) plyr:::splitter_a(arr,3) I understand that I can write a for loop to make it happen but I am searching for a better solution. Thanks in advance. -Sean [[alternative HTML version deleted]]
Hadley Wickham
2011-Jan-17 23:07 UTC
[R] how to cut a multidimensional array along a chosen dimension and store each piece into a list
On Mon, Jan 17, 2011 at 2:20 PM, Sean Zhang <seanecon at gmail.com> wrote:> Dear R-Helpers, > > I wonder whether there is a function which cuts a multiple dimensional array > along a chosen dimension and then store each piece (still an array of one > dimension less) into a list. > For example, > > arr <- array(seq(1*2*3*4),dim=c(1,2,3,4)) ?# I made a point to set the > length of the first dimension be 1to test whether I worry about drop=F > option. > > brkArrIntoListAlong <- function(arr,alongWhichDim){ > #### > return(outlist) > } > > I have tried splitter_a in plyr package but does not get what I want. > > library(plyr) > plyr:::splitter_a(arr,3)We'll you're really not supposed to call internal functions - you probably want: alply(arr, 3) but you don't say what is wrong with the output. Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/