 This is a quick tip video about custom functions in R. Functions are reusable pieces of code. You can write custom functions to run multiple times, for example when iterating over a data frame to make several visualizations or models. In this case, iterating means using the per library in one of the many map functions. In this video series, I explain how to use the per map functions to iterate custom functions over data frames. The series will become progressively more fluent in data wrangling examples, but here I show a building block data wrangling tip that will help you leverage R as a functional programming language. As always, see the video description for a link to the code in the GitHub repository. What you see in the code chunk that begins in online 19 are two functions. Each takes a single argument. The first function multiplies a number by two. The second function is really just a silly example. It simply allows me to change the name of the base R plot function. So now the base R function plot and my custom function plot cars, they do the same thing. In line 31 and 32, I invoke these functions. Line 31 takes a single argument and multiplies by two. You can look up in the function to see how it works. Line 32 takes a two variable data frame as an argument. And here I use the onboard cars data frame, a two dimensional data frame with two numeric vectors. Let's see how the custom function plot cars can be used. In the code chunk that begins in line 36, I subset the Star Wars data frame into a two dimensional data frame consisting of height and mass. In 41, I reuse my custom function and give it a different data frame as a single argument. The code chunk that begins at line 45 demonstrates a shortcut to making a custom function a different kind of custom function. We've already made two. As of this writing, this style function only works with the McGridder style pipe conjunction. The dot in line 46 represents whatever is passed from the left-hand side of the pipe. Then you see in line 55 how the custom function is implemented. Beginning at line 62, you can see the more formal, that is, base R way to write the exact same function as I wrote above, only this time with the base R pipe. That is, the pipe greater than symbol. Executing this function found in line 73 when invoking the data sentence pipe that begins at line 72 yields the same results as above. Now I suspect that a best practice is to use this style over that from the example above. But to be honest, I don't know much about the implications of either style. The dot style above only works with the McGridder pipe. Probably this other one is more portable. Okay. For those that have watched my Regex stringer video, you'll notice that in line 81 it demonstrated a more complicated Regex pattern. And by its very nature, I accomplished the same thing as the previous two custom functions. To be fair, custom functions may be more readable, but a good Regex pattern can be very satisfying in its own way. But this brings me to share some advice. The general rule of thumb is to write a custom function anytime you find yourself composing and executing the same operation more than three times. With nothing else, writing and leveraging the custom function will reduce your chances of making a silly typo error or repetitive tests. And the common wisdom is that custom functions allow you to make code more readable, while composing your computational pipe with more explicitly descriptive function names. So the pro tip is recognize that R is a functional programming image. Now you're able to compose custom functions. All right, let's dig a little deeper. If you plan to iterate over data frames, which honestly is the topic of this series of videos, you're going to need to learn a bit more about how to compose functions. What I want to talk to you about now is tidy evaluation and data masking. The examples will help me explain. So another thing to understand about functions in a tidyverse context is that the tidyverse uses tidy evaluation and data masking. These two concepts undergird the guts of the tidyverse. Newcomers to the tidyverse dialect often don't realize this undergirding exists. Rather, what you know is that composing code in tidyverse is more convenient than composing code in other high level languages. In addition to easier coding, this makes tidyverse code more fluid because code can be less redundant, both in terms of needing fewer quotation marks and needing fewer explicit references to environment variables. Now I don't want to get too deep on this, so feel free to read some of the references in the descriptions, but let me briefly define an environment variable and a data variable. An environment variable, for example, gets something assigned to it with the assignment operator, such as the name of the table, for example, in line 92, who underscore df is an environment variable. A data variable is a variable within a data frame. And the trick is that referencing a data variable in a tidy evaluation data masking context happens indirectly. So you have to know a bit more to write good tidyverse functions. By way of demonstration, in line 94, we have a custom function that takes one argument, an environment variable that happens to be a table composed of data variables. When we call the custom function in line 100, we get a scatter plot. The function is hard coded with two variable names, height and mass. You see that in line 96. If I want to write my function in line 94 to accept different data variable names, I need to capture the data variable name as an argument in line 94 and embrace the temporary functional variable with curly curly braces in line 96. And then I need to pass the data variable name when I call the custom function in line 100. Here you see that I didn't embrace my data variable in line 96. So I get an error that the object that is mass is not found. But now when I embrace the data variable argument in line 96, then I can pass mass for another data variable. So let's try birth underscore year. The code chunk at line 118 is another example of data masking. The dot dot dot construction in line 119 is a way to accept additional arguments for a function. We're not going to get into that. So if you want to look that up in the advanced R book by Wiccum. Going forward in this video and in the future videos in this series, let's talk about how to apply custom functions efficiently in an iterative fashion. To do that, we'll use the per library and a few of the map underscore functions. Okay, so in line 132 you'll see the map underscore dbl function. We're going to use this to iterate over each element of a vector. The argument past the comma is what's known as an anonymous function. So called anonymous because we have not assigned the function a name. We could put a custom function name in this argument space and we could use one of the earlier custom functions we wrote that made sense to our overall goal. We just wouldn't need the tilde since it wouldn't be an anonymous function. Okay, so the anonymous function uses the dot x as a pronoun to refer to the current element of the vector that is being looped over. That is the current element in the map function. Don't loop over each element one at a time consecutively and dot x refers to each one of those one at a time consecutively. It will then apply to that element the function expression of, in this case, multiply by two. Notice that the function we're using is map underscore dbl. This underscore dbl tells the map function. We want a double floating point data type in return. There are several different underscore suffixes to the map function. If we remove the suffix, we get the default back, which is a list. If we use underscore chr, we get a character vector. There are also suffixes to return data frames, as you may have seen in the very first video in this series. But now for the cool part. If we use this function in a data frame, we can iterate a function over each row of a vector in the data frame. In the next video, we'll learn how to nest group data into a data frame. And in the last two videos, we'll return to our opening examples in the beginning of this series and learn how to iterate over grouped rows of data to generate visualizations and data models. Stay tuned for later videos where we demonstrate all of these case studies put into one. We use regex pivot longer, we iterate with map, and we write custom functions. We even use a for loop.