R Tutorial : Converting scripts into functions

preview_player
Показать описание

---

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.

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.

Recall the four sets of code for analyzing the test scores. Let's see how all these scripts could be wrapped into a function.

To convert a script into a function, you start by writing this template, with the word "function" followed by parentheses and braces.

Next, you paste your script inside the braces.

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.

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".

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.

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.

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.

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.

The importing test scores function was a little tricky, so let's begin with something simpler.

#DataCamp #RTutorial #IntroductiontoWritingFunctionsinR
Рекомендации по теме