1. Converting scripts into functions
A great workflow is to write a script, then convert any repeated chunks of code in that script to functions. In this video, you'll see how to do that.
2. A basic function template
The syntax for defining a function follows this template.
In R, functions are a type of variable, so you assign functions using the left arrow, just like anything else.
That first line, with the word "function" followed by arguments one and two inside parentheses, is called the function signature. This names the variables that are passed into the function. It's the user interface to the function.
The bits inside the curly braces are called the body of the function. This is where the calculation happens.
3. Revisiting the 4 test analyses
Recall the four sets of code for analyzing the test scores. Let's see how all these scripts could be wrapped into a function.
4. Make a template
To convert a script into a function, you start by writing this template, with the word "function" followed by parentheses and braces.
5. Paste your script into the body
Next you paste your script inside the braces.
6. Choose the arguments
The third step is to choose the arguments to the function. The arguments should be any bits of code that aren't duplicated across your different chunks of code.
In this case, the only bit of code that changed between the four blocks was the filename, so that's your only argument.
7. Replace specific values with arguments
Now you replace the specific instances of values inside the body with the argument names.
Here, "test_scores_geography-dot-csv" is replaced with the argument "filename".
8. Generalize variable names
The variable names test_scores_geography_raw and test_scores_geography_clean are too specific now that the code is supposed to work with test data from any subject. Dropping the word geography from each makes them more general.
9. Remove the final assignment
Functions in R return the last value that is calculated in their body, so you don't need to assign that last value: it will automatically become the result.
Here, we drop the final assignment to test_scores_clean.
10. Use your function
To use your function, you can just call it like you would any other function.
Notice how each subject that you add only requires one more line of code, and if you need to change how the data is imported, you only need to change it in one place.
11. Arguments of sample()
In the next exercises, you'll use the sample function. Its arguments are shown on-screen for reference.
This samples a vector named x, and has options for the number of samples to return, whether you want to sample with replacement, and whether to weight the sampling to return more of some values.
12. Let's practice!
The importing test scores function was a little tricky, so let's begin with something simpler.