The Vectorize()
and the apply()
functions in R
can often be used to accomplish the same goal. I usually prefer vectorizing a function for readability reasons, because the main calling function is related to the task at hand while sapply
is not. It is also useful to Vectorize()
when I am going to be using that vectorized function multiple times in my R code. For instance:
a <- 100
b <- 200
c <- 300
varnames <- c('a', 'b', 'c')
getv <- Vectorize(get)
getv(varnames)
vs
sapply(varnames, get)
However, at least on SO I rarely see examples with Vectorize()
in the solution, only apply()
(or one of it's siblings). Are there any efficiency issues or other legitimate concerns with Vectorize()
that make apply()
a better option?
Vectorize
is just a wrapper formapply
. It just builds you anmapply
loop for whatever function you feed it. Thus there are often easier things do to thanVectorize()
it and the explicit*apply
solutions end up being computationally equivalent or perhaps superior.Also, for your specific example, you've heard of
mget
, right?To add to Thomas's answer. Maybe also speed?