Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a vectorized function in R

Tags:

r

As the title, I'd like to know how to define a vectorized function in R.

  • Is it just by using a loop in the function?
  • Is this method efficient?
  • And what's the best practice ?
like image 487
zjffdu Avatar asked Aug 15 '12 07:08

zjffdu


People also ask

How do you create a vector function in R?

How to Create Vector in R? Vectors are generally created using the c() function. Since, a vector must have elements of the same type, this function will try and coerce elements to the same type, if they are different. Coercion is from lower to higher types from logical to integer to double to character.

What is a vectorized function in R?

Most of R's functions are vectorized, meaning that the function will operate on all elements of a vector without needing to loop through and act on each element one at a time. This makes writing code more concise, easy to read, and less error prone.

How do I know if a function is vector in R?

To identify if an R object is a vector , I can use is. vector(x) , which returns TRUE if x is a vector or False otherwise.

Why do you vectorize a function?

The vectorize function is provided primarily for convenience, not for performance. The implementation is essentially a for loop. If otypes is not specified, then a call to the function with the first argument will be used to determine the number of outputs.


1 Answers

A loop at the R level is not vectorized. An R loop will be calling the same R code for each element of a vector, which will be inefficient. Vectorized functions usually refer to those that take a vector and operate on the entire vector in an efficient way. Ultimately this will involve some for of loop, but as that loop is being performed in a low-level language such as C it can be highly efficient and tailored to the particular task.

Consider this silly function to add pairwise the elements of two vectors

sillyplus <- function(x, y) {     out <- numeric(length = length(x))     for(i in seq_along(x)) {         out[i] <- x[i] + y[i]     }     out } 

It gives the right result

R> sillyplus(1:10, 1:10)  [1]  2  4  6  8 10 12 14 16 18 20 

and is vectorised in the sense that it can operate on entire vectors at once, but it is not vectorised in the sense I describe above because it is exceptionally inefficient. + is vectorised at the C level in R so we really only need 1:10 + 1:10, not an explicit loop in R.

The usual way to write a vectorised function is to use existing R functions that are already vectorised. If you want to start from scratch and the thing you want to do with the function doesn't exist as a vectorised function in R (odd, but possible) then you will need to get your hands dirty and write the guts of the function in C and prepare a little wrapper in R to call the C function you wrote with the vector of data you want it to work on. There are ways with functions like Vectorize() to fake vectorisation for R functions that are not vectorised.

C is not the only option here, FORTRAN is a possibility as is C++ and, thanks to Dirk Eddelbuettel & Romain Francois, the latter is much easier to do now with the rcpp package.

like image 117
Gavin Simpson Avatar answered Sep 26 '22 03:09

Gavin Simpson