I found an old example of lapply which was apparantly written in R and not in C.
la1 <- function(X, FUN, ...) {
FUN <- match.fun(FUN)
if (!is.list(X))
X <- as.list(X)
rval <- vector("list", length(X))
for(i in seq(along = X))
rval[i] <- list(FUN(X[[i]], ...))
names(rval) <- names(X) # keep `names' !
return(rval)
}
Thus, even if a package or function exists, you can't get better than following
lapply2 <- function (X, FUN, ...) {
names <- names(X)
FUN <- match.fun(FUN)
if (!is.vector(X) || is.object(X)) {
X <- as.list(X)
}
names(X) <- names
.Internal(lapply(X, FUN))
}
It would be nice though to know why the names were dropped in the first place.