Skip to content
Prev 106156 / 398506 Next

sapply problem

Joerg van den Hoff said the following on 12/14/2006 7:30 AM:
Try this:

a <- list(list(df = data.frame(A = 1, B = 2, C = 3)),
           list(df = data.frame(A = 4, B = 5, C = 6)))
b <- do.call("rbind", sapply(a, "[", "df"))
b
I think it does as it much as it knows how. I think you might believe 
that matrix elements can only contain numeric values. This is not a 
valid assumption. Take this example:

 > a <- list(1)
 > b <- list(2)
 > (m <- matrix(c(a, b), 2, 1))
      [,1]
[1,] 1
[2,] 2
 > class(m[1, 1])
[1] "list"
 > class(m[2, 1])
[1] "list"

HTH,

--sundar