Chapter 25. Functions
Introduction
One of the best ways to improve your reach as a data scientist is to write functions. Functions allow you to automate common tasks in a more powerful and general way than copy and pasting. Writing a function has three big advantages over using copy and paste:
You can give a function an evocative name that makes your code easier to understand.
As requirements change, you need to update code only in one place, instead of many.
You eliminate the chance of making incidental mistakes when you copy and paste (i.e., updating a variable name in one place but not in another).
It makes it easier to reuse work from project to project, increasing your productivity over time.
A good rule of thumb is to consider writing a function whenever you’ve copied and pasted a block of code more than twice (i.e., you now have three copies of the same code). In this chapter, you’ll learn about three useful types of functions:
- Vector functions take one or more vectors as input and return a vector as output.
- Data frame functions take a data frame as input and return a data frame as output.
- Plot functions take a data frame as input and return a plot as output.
Each of these sections includes many examples to help you generalize the patterns that you see. These examples wouldn’t be possible without the help of the folks of Twitter, and we encourage you to follow the links in the comment to see original inspirations. You might also want to read the original motivating tweets ...
Get R for Data Science, 2nd Edition 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.