<- function(input_matrix){
column_sums <- ncol(input_matrix)
n_cols <- rep(NA, n_cols)
sums for(i in 1:n_cols){
<- sum(input_matrix[,i])
sums[i]
}return(sums)
}
Live code:
Creating functions in R
Here we will learn how to write functions in R
. Functions are extremely helpful for automating commons tasks that you might use often (e.g. computing the RMSE for a set of predictions). If you’re going to use the same block of code more than twice, you should consider writing a function!
There are three key steps to creating a new function:
- Picking a NAME for the function
- Listing the INPUTS/ARGUMENTS to the function called
function()
- Placing the code you have developed in the BODY of the function (between the sets of curly braces
{ }
that immediately followfunction()
- Making sure to
return()
the output from the function
- Making sure to
Example
For example, suppose that I want to create a function that takes in a matrix and returns the sums of the values within each column. (There is a function that already does this, but pretend there isn’t!) The function might look like this:
This looks scary, but let’s break it down.
The name of the function is
column_sums
.R
knows I want to create a function because I use thefunction()
function, where I specify that my function requires a single input that will be referred to asinput_matrix
.The code within the body specifies how I will use
input_matrix
to calculate the column sums.I finish the function by
return()
-ing a vector.
Let’s test this out: I will create a matrix of numbers, and then use/call my function as I normally would any R
function:
<- matrix(1:10, nrow = 2, ncol = 5)
my_mat my_mat
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
column_sums(my_mat)
[1] 3 7 11 15 19
Let’s confirm this is the correct output with the pre-provided R
function colSums()
:
colSums(my_mat)
[1] 3 7 11 15 19
How might we code a function that calculates the squared error between two values? Try it yourself, then check here to see if your code generally agrees with mine:
Code
<- function(x1, x2){
squared_error <- (x1 - x2)^2
ret return(ret)
}
Code
# test your code: you should get 16 for passing in -2 and 2
squared_error(-2, 2)
Your turn!
Feel free to try any and all of the following:
- Write a function that takes in a temperature in degrees Fahrenheit and returns the temperature in degrees Celsius. For reference, the conversion is \((\text{degreesF} - 32) * \frac{5}{9}\). Give your function an appropriate name.
- Check to make sure your function works by passing in \(32^\circ\) F. You should get 0 back!
- Write a more complicated version of Exercise 1 where the function takes in two inputs: 1) a temperature (in either Fahrenheit or Celsius) and 2) a string or Boolean (your choice!) that denotes if you want to convert to Fahrenheit or Celsius. Your function should return the correct conversion based on the user’s inputs. Note: the conversion from Celsius to Fahrenheit is \(\text{degreesC} * \frac{9}{5} + 32\).
- Writ a function called
get_rmse()
that takes in two vectors as inputs: one of predictions, and one of true values. Your function should calculate and return the root mean squared error (see slides for equation). - Last week we practiced coding
for()
loops by obtaining the factorial of a given positive integer. Create a function where the user specifies the integer they want to find the factorial of, and return the factorial.