Chapter 11. Work Smarter, Not Harder – Efficient and Elegant R Code

In this chapter, we will cover recipes for doing the following without explicit iteration:

  • Exploiting vectorized operations
  • Processing entire rows or columns using the apply function
  • Applying a function to all elements of a collection with lapply and sapply
  • Applying functions to subsets of a vector
  • Using the split-apply-combine strategy with plyr
  • Slicing, dicing, and combining data with data tables

Introduction

The R programming language, being procedural, provides looping control structures. Most people will therefore tend to automatically use these control structures in their own code and end up with performance issues because R handles loops very inefficiently. Serious number crunching ...

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.