14

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?

smci
  • 32,567
  • 20
  • 113
  • 146
andrew
  • 2,524
  • 2
  • 24
  • 36
  • 1
    Hmm... did you run your own example? I'm already seeing one problem with `Vectorize` there... – David Arenburg Aug 01 '14 at 13:45
  • Well I guess that starts to answer my question. Under the hood it looks like `Vectorize()` is just a wrapper for `mapply()`. – andrew Aug 01 '14 at 13:54
  • Since your example is obviated with `mget()`, can you rewrite with a better example? – smci May 28 '18 at 09:28

2 Answers2

15

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?

Thomas
  • 43,637
  • 12
  • 109
  • 140
14

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)

enter image description here

Eric Fail
  • 8,191
  • 8
  • 72
  • 128