[R] Vectorizing a function: what does it mean?

Ron Michael ron_michael70 at yahoo.com
Mon May 9 17:57:53 CEST 2011


Dear all, I would really appreciate if somebody can help me to understand what does the phrase "Vectorize your function" mean? And what is the job of Vectorize() function in doing that? I have read many threads where experts suggest to Vectorize the function, which will speed up entire calculation (and more elegant ofcourse.)

I used to think that vectorizing function means, to create a function such a way so that if input(s) are vector then this function will also return a vector output (without having any extra effort like use of 'for' loop etc.) Is this understanding correct?

Here I have tried 2 essentially similar functions:

> fn1 <- function(x,y,z) return(x+y+z)
> fn2 <- Vectorize(function(x,y,z) return(x+y+z), SIMPLIFY=TRUE)
> fn1(1:3, 4:6,5)
[1] 10 12 14
> fn2(1:3, 4:6,5)
[1] 10 12 14


You see that, fn1() and fn2() is giving same answer (vectorized?) despite of the fact that fn1() is not wrapped within Vectorize() function. Therefore I want to know: what additional thing this Vectorize() function brings on the table?

Thanks for your time.



More information about the R-help mailing list