[R] abind, but on lists?

David Winsemius dwinsemius at comcast.net
Thu Sep 3 05:07:02 CEST 2009


On Sep 2, 2009, at 10:50 PM, Peter Meilstrup wrote:

> I'm trying to massage some data from Matlab into R. The matlab file  
> has a "struct array" which when imported into R using the R.matlab  
> package,  becomes an R list with 3+ dimensions, the first of which  
> corresponds to the structure fields, with corresponding row names,  
> and the second and third+ dimensions correspond to the dimensions of  
> the original struct array (as matlab arrays always have 2+  
> dimensions). For example a 1x1 matlab struct array with three fields  
> becomes:
>
> > data
> , , 1
>
>          [,1]
> trials    List,1002
> beforeRun List,8
> afterRun  List,8
>
> If I load two data files, I would want to concatenate them into a  
> single list. cbind() doesn't preserve the row names, possibly  
> because the array has three dimensions?

No. cbind makes matrices. Arrays and matrices are required to have all  
elements of the same type, so any coercive function returning an array  
would force the elements to have the "lowest common denominator" type,  
which is "list" in this case.

>
> > cbind(data1, data2)
>     data1     data2
> [1,] List,1002 List,479
> [2,] List,8    List,8
> [3,] List,8    List,8
>
> Then I looked into abind(), which is even more puzzling: it  
> preserves the names and dimensions correctly, but converts the  
> entries into strings?!

Must be from a non-base package or a devel version. I get
 > abind
Error: object 'abind' not found
>
> > abind(list(data1, data2), along=2, force.array=FALSE)
> , , 1
>
>          [,1]               [,2]
> trials    "0"                "1"
> beforeRun "2079647.50207592" "0"
> afterRun  "1"                "0"
>
> Is there a quick way to accomplish the effect I want? I want an  
> output that looks like:
>
> > somebind(data1, data2, along=2)

How about:

list(data1, data2)

... or in this case, because you are starting with two list objects,  
you could get the same effect with:

c(data1, data2)


> , , 1
>
>          [,1]               [,2]
> trials    List,1002 List,479
> beforeRun List,8    List,8
> afterRun  List,8    List,8
>
> Cheers,
> Peter
>
> ___________

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list