y <- sapply(vec, fun)
f <- function(x) { x^2 } v <- c(4, 2, 5, 8, 7) sapply (v, f) # # [1] 16 4 25 64 49
sapply
returns a matrix. vec <- c('abc', 'd', 'ef'); mat <- sapply( vec, function(elem) { c(nchar(elem), toupper(elem)) }); class(mat); # # matrix mat; # # abc d ef # [1,] "3" "1" "2" # [2,] "ABC" "D" "EF"