Vectorize() vs apply()

2019-02-06 13:27发布

问题:

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?

回答1:

Vectorize is just a wrapper for mapply. It just builds you an mapply loop for whatever function you feed it. Thus there are often easier things do to than Vectorize() 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?



回答2:

To add to Thomas's answer. Maybe also speed?

    # install.packages(c("microbenchmark", "stringr"), dependencies = TRUE)
require(microbenchmark)
require(stringr)

Vect <- function(x) { getv <- Vectorize(get); getv(x) }
sapp <- function(x) sapply(x, get)
mgett <- function(x) mget(x)
res <- microbenchmark(Vect(varnames), sapp(varnames), mget(varnames), times = 15)

## Print results:
print(res)
Unit: microseconds
           expr     min       lq  median       uq     max neval
 Vect(varnames) 106.752 110.3845 116.050 122.9030 246.934    15
 sapp(varnames)  31.731  33.8680  36.199  36.7810 100.712    15
 mget(varnames)   2.856   3.1930   3.732   4.1185  13.624    15


### Plot results:
boxplot(res)