The Vectorize() and apply() functions in R can often be used to achieve the same goal. I usually prefer to vectorize a function for readability, because the main calling function is connected to the task at hand, and sapply is not. It is also useful for Vectorize() when I will use this vectorized function several times in my R code. For example:
a <- 100 b <- 200 c <- 300 varnames <- c('a', 'b', 'c') getv <- Vectorize(get) getv(varnames)
against
sapply(varnames, get)
However, at least on SO, I rarely see examples with Vectorize() in a solution, only apply() (or one of its siblings). Are there any performance issues or other legitimate issues with Vectorize() that make apply() better option?
r
andrew
source share