Advertisement
Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

How to Vectorize Your Functions in R

Vectorized functions are a very useful feature of R, but programmers who are used to other languages often have trouble with this concept at first. A vectorized function works not just on a single value, but on a whole vector of values at the same time.

Your natural reflex as a programmer may be to loop over all values of the vector and apply the function, but vectorization makes that unnecessary. Trust us: When you start using vectorization in R, it’ll help simplify your code.

To try vectorized functions, you have to make a vector. You do this by using the c() function, which stands for concatenate. The actual values are separated by commas.

Here’s an example: Suppose that Granny plays basketball with her friend Geraldine, and you keep a score of Granny’s number of baskets in each game. After six games, you want to know how many baskets Granny has made so far this season. You can put these numbers in a vector, like this:

> baskets.of.Granny <- c(12,4,4,6,9,3)
> baskets.of.Granny
[1] 12 4 4 6 9 3

To find the total number of baskets Granny made, you just type the following:

> sum(baskets.of.Granny)
[1] 38

You could get the same result by going over the vector number by number, adding each new number to the sum of the previous numbers, but that method would require you to write more code and it would take longer to calculate. You won’t notice it on just six numbers, but the difference will be obvious when you have to sum a few thousand of them.

In this example of vectorization, a function uses the complete vector to give you one result. Granted, this example is trivial (you may have guessed that sum() would accomplish the same goal), but for other functions in R, the vectorization may be less obvious.

A less obvious example of a vectorized function is the paste() function. If you make a vector with the first names of the members of your family, paste() can add the last name to all of them with one command, as in the following example:

> firstnames <- c("Joris", "Carolien", "Koen")
> lastname <- "Meys"
> paste(firstnames,lastname)
[1] "Joris Meys"  "Carolien Meys" "Koen Meys"

R takes the vector firstnames and then pastes the lastname into each value. How cool is that? Actually, R combines two vectors. The second vector — in this case, lastname — is only one value long. That value gets recycled by the paste() function as long as necessary.

You also can give R two longer vectors, and R will combine them element by element, like this:

> authors <- c("Andrie","Joris")
> lastnames <- c("de Vries","Meys")
> paste(authors,lastnames)
[1] "Andrie de Vries" "Joris Meys"

No complicated code is needed. All you have to do is make the vectors and put them in the function.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win an iPad Mini. Enter to win now!