Exploiting vectorized operations

Some R functions can operate on vectors as a whole. The function can either be a built-in R function or a custom function. In your own code, before you resort to a loop to process all elements of a vector, see whether you can exploit an existing vectorized function.

Getting ready

If you have not already downloaded the files for this chapter, do it now and ensure that the auto-mpg.csv file is in your R working directory.

How to do it...

To exploit vectorized operations follow these steps:

  1. Operate on all elements of vector(s) without explicit iteration (vectorized operations):
    > first.name <- c("John", "Jane", "Tom", "Zach") > last.name <- c("Doe", "Smith", "Glock", "Green") > # The paste function below operates on vectors ...

Get R: Recipes for Analysis, Visualization and Machine Learning now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.