[R] abind, but on lists?

Peter Meilstrup peter.meilstrup at gmail.com
Thu Sep 3 07:20:40 CEST 2009


On Sep 2, 2009, at 8:47 PM, Richard M. Heiberger wrote:

> Almost certainly, abind is what you need for the task.
>
> Please dput() your matlab objects and send that to the list.
> That will make your example reproducible.
>
> Rich

 From the documentation and behavior, abind seems to really not want  
to work on lists.

Here is a dput of some list arrays that demonstrate the problem.

 > data1
, , 1

     [,1]      [,2]
foo 1         4
bar Numeric,3 Numeric,3
baz "quux"    "n"

 > dput(data1)
structure(list(1L, c(3, 4, 5), "quux", 4L, c(3, 5, 9), "n"), .Dim =  
c(3L,
2L, 1L), .Dimnames = list(c("foo", "bar", "baz"), NULL, NULL))

 > data2
, , 1

     [,1]
foo 1
bar Numeric,5
baz "qux"

 > dput(data2)
structure(list(1, c(2, 3, 4, 6, 9), "qux"), .Dim = c(3L, 1L,
1L), .Dimnames = list(c("foo", "bar", "baz"), NULL, NULL))

abind() decides that it wants to coerce all lists into character,  
among other failures (e.g. how did "quux" wind up in the second row?:)

 > library(abind)
 > abind(list(data1,data2), along=2, force.array=FALSE)
, , 1

     [,1] [,2]   [,3]
foo "1"  "5"    "3"
bar "3"  "quux" "5"
baz "4"  "4"    "9"

What I want to happen:

 > goal
, , 1

      [,1]      [,2]      [,3]
[1,] 1         4         1
[2,] Numeric,3 Numeric,3 Numeric,5
[3,] "quux"    "n"       "qux"

 > dput(goal)
structure(list(1L, c(3, 4, 5), "quux", 4L, c(3, 5, 9), "n", 1,
     c(2, 3, 4, 6, 9), "qux"), .Dim = c(3L, 3L, 1L))


--Peter




More information about the R-help mailing list