Writing functions
1. Writing functions
Writing your own functions in R is a big step towards making your code more efficient, understandable, and reusable. Functions can be very detailed,2. Function structure
but the basics are pretty simple. You define a function by writing the custom name of the function, followed by an assignment operator, then you use the keyword function to tell R that you are creating a new function, specify all of your arguments inside the parenthesis, and finally write the code that your function performs inside the function body.3. Add one
This example constructs a simple function named add_one. Obviously this is a simple example, but with it we can explore some useful features of functions. The user enters the value for x, and inside the function 1 is added to it, and then the new value is returned. The return function specifies what is returned, but it actually isn't needed here. If you leave off the return function, then by default the function will return the last line that gets executed. That would allow us to simplify the function to this more concise code. Now there is no need for the intermediate object x_plus_one. R runs x + 1 and instantly returns that value. Currently, the function is pretty inflexible in that it can only add 1.4. Using an optional argument
We can generalize this by adding an optional argument, value, that will allow the user to specify what value gets added to x. Setting the optional argument to a default value of 1 keeps the original functionality, but opens it up to more flexible use. The only other modification is to replace the 1 inside the function body with value. The examples shown demonstrate using the more general function with and without the optional argument. This is often the way you will create your functions in the real world. First, you solve a very specific problem, then you wrap it up into a function, and finally you generalize it to work with other similar problems.5. Calculating arithmetic returns
What if you wanted to create a function that calculated daily arithmetic stock returns for you? Given the vector of stock prices, prices, how might you solve this specific problem, to then generalize it into a function? The general formula for arithmetic return is the stock price at the current time, minus yesterday's stock price, divided by yesterday's stock price. (S_t - S_t-1) / S_t-1. Let's first try and calculate the numerator. The diff function in R will calculate those differences for us. diff(prices) returns a vector of differences which is one element shorter than prices. Okay, so what about the denominator? We need the vector of stock prices, excluding the last date. To find the position of the last element of the vector, we can use length(y), and then use a negative sign inside brackets to tell R that we want to exclude that element. To find the returns, we just divide the two results. Great, let's wrap this up into a function called arith_returns.6. Calculating arithmetic returns
This will have 1 argument, x as the vector of returns. To keep things simple, we won't do any checks to make sure x is in the correct form, but this would certainly be good practice. The calculation performed on x is exactly what we just did with prices. Take the diff, and divide by the prices without the last element. Calling arith_returns(prices), we get the same answer as before. There is an intangible benefit of wrapping this logic into a function with a descriptive name. You, as the reader of the code, know exactly what arith_returns is supposed to do just by its name, which helps future you if you have to look back at this months down the road.7. Let's practice!
You'll have lots of fun writing your own functions in the exercises. This is a big step! Good luck!Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.