sapply() stems from S / S+ times and hence has a long tradition.
In spite of that I think that it should be enhanced...
As the subject mentions, sapply() produces a matrix in cases
where the list components of the lapply(.) results are of the
same length (and ...).
However, it unfortunately "stops there".
E.g., if you *nest* two sapply() calls where the inner one
produces a matrix, very often the logical behavior would be for
the outer sapply() to stack these matrices into an array of
rank 3 ["array rank"(x) := length(dim(x))].
However it does not do that, e.g., an artifical example
p0 <- function(...) paste(..., sep="")
myF <- function(x,y) {
stopifnot(length(x) <= 3)
x <- rep(x, length.out=3)
ny <- length(y)
r <- outer(x,y)
dimnames(r) <- list(p0("r",1:3), p0("C", seq_len(ny)))
r
}
and
(v <- structure(10*(5:8), names=LETTERS[1:4]))
A B C D
50 60 70 80
if we let sapply() not simplify, we see the list of same size
matrices it produes:
-----------
In the best of all worlds, the default would be 'ARRAY = TRUE',
but of course, given the long-standing different behavior,
it seem much too "risky", and my proposal includes remaining
back-compatible with default ARRAY = FALSE.
Martin Maechler,
ETH Zurich