Say your function in sapply returns a matrix with a variable number of rows. For example

ff <- function( i ) matrix( i, ncol= 3, nrow= i ) pipa <- sapply( 1:3, , simplify= "array" )

sapply is too stupid to see the pattern here (or maybe I don’t know how to cast the return value into an appropriate shape…). The result of the above is a list:

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

However, we can turn this list of matrices into a simple matrix using Reduce:

pipa <- Reduce( function( ... ) rbind( ... ), pipa )

Advertisements