Create functions in R (2024)

HOME INTRODUCTION TO R FUNCTION R

Introduction to R R basics

Create functions in R (1)

R programming language allows the user create their own new functions. In this tutorial you will learn how to write a function in R, how the syntax is, the arguments, the output, how the return function works, and how make a correct use of optional, additional and default arguments.

How to write a function in R language? Defining R functions

The base R functions doesn’t always cover all our needs. In order to write a function in R you first need to know how the syntax of the function command is. The basic R function syntax is as follows:

function_name <- function(arg1, arg2, ... ) { # Code}

In the previous code block we have the following parts:

  • arg1, arg2, … are the input arguments.
  • # Code represents the code to be executed within the function to calculate the desired output.

The output of the function can be a number, a list, a data.frame, a plot, a message or any object you want. You can also assign the output some class, but we will talk about this in other post with the S3 classes. The last is specially interesting when writing functions for R packages.

Creating a function in R

To introduce R functions we will create a function to work with geometric progressions. A geometric progression is a succession of numbers \(a_1, a_2, a_3\) such that each of them (except the first) is equal to the last multiplied by a constant r called ratio. You can verify that,

\(a_2 = a_1 \cdot r; \qquad a_3 = a_2 \cdot r = a_1 \cdot r^2; \dots\)

Hence, generalizing this process you can obtain the general term

\(a_n = a_1 \cdot r^{n-1}\).

You can also verify that the sum of the n terms of the progression is

\(S_n = a_1 + \dots + a_n = \frac{a_1(r^n - 1)}{r-1}\).

With this in mind you can create the following function,

an <- function(a1, r, n){ a1 * r ** (n - 1)}

that calculates the general term \(a_n\) of a geometric progression giving the parameters \(a_1\), the ratio r and the value n. In the following block we can see some examples with its output as comments.

an(a1 = 1, r = 2, n = 5) # 16an(a1 = 4, r = -2, n = 6) # -128

With the previous function you can obtain several values of the progression passing a vector of values to the argument n.

an(a1 = 1, r = 2, n = 1:5) # a_1, ..., a_5an(a1 = 1, r = 2, n = 10:15) # a_10,..., a_15

You can also calculate the first n elements of the progression with sn function, defined below.

sn <- function(a1, r, n){ a1 * (r ** n-1)/(r - 1)}
sn(a1 = 1, r = 2, n = 5) # 31# Equivalentvalues <- an(a1 = 1, r = 2, n = 1:5)valuessum(values) # 31

Input arguments in R functions

Arguments are input values of functions. As an example, on the function we created before we have three input arguments named a1, r and n. There are several considerations when dealing with this type of arguments:

  • If you maintain the input order, you don’t need to call the argument names. As an example, the following calls are equivalent.
an(1, 2, 5) # Returns 16an(a1 = 1, r = 2, n = 5) # Returns 16
  • If you name the arguments, you can use any order.
an(r = 2, n = 5, a1 = 1) # Returns 16an(n = 5, r = 2, a1 = 1) # Returns 16
  • You can make use of the args function to know the input arguments of any function you would like to use.
args(an)
  • If you call the function name, the console will return the code of the function.

Note that sometimes you won’t be able to see the source code of a function if it is not written in R.

Default arguments for functions in R

Sometimes it is very interesting to have default function arguments, so the default values will be used unless others are included when executing the function. When writing a function, such as the one in our example,

function_name <- function(arg1, arg2, arg3 ) { # Code}

if you want arg2 and arg3 to be a and b by default, you can assign them in the arguments of your R function.

function_name <- function(arg1, arg2 = a, arg3 = b) { # Code}

We will illustrate this with a very simple example. Consider, for instance, a function that plots the cosine.

cosine <- function(w = 1, min = -2 * pi, max = 2 * pi) { x <- seq(-2 * pi, 2 * pi, length = 200) plot(x, cos(w * x), type = "l")}

Note that this is not the best way to use a function to make a plot. See S3 classes for that purpose.

If you execute cosine() the plot of cos(x) will be plotted by default in the interval [-2π, 2π]. However, if you want to plot the function cos(2x) in the same interval you need to execute cosine(w = 2). Let’s see some examples:

# One row, three columnspar(mfcol = c(1, 3))cosine()cosine(w = 2)cosine(w = 3, min = -3 * pi)

Create functions in R (2)

Additional arguments in R

The argument (dot-dot-dot) allows you to freely pass arguments that will use a sub-function inside the main function. As an example, in the function,

cosine <- function(w = 1, min = -2 * pi, max = 2 * pi, ...) { x <- seq(-2 * pi, 2 * pi, length = 200) plot(x, cos(w * x), ...)}

the arguments inside will be used by the plot function. Let’s see a complete example:

par(mfcol = c(1, 2))cosine(w = 2, col = "red", type = "l", lwd = 2)cosine(w = 2, ylab = "")

Create functions in R (3)

The R return function

By default, the R functions will return the last evaluated object inside it. You can also make use of the return function, which is especially important when you want to return one object or another, depending on certain conditions, or when you want to execute some code after the object you want to return. It is worth to mention that you can return all types of R objects, but only one. For that reason it is very usual to return a list of objects, as follows:

asn <- function(a1 = 1, r = 2, n = 5) { A <- an(a1, r, n) S <- sn(a1, r, n) ii <- 1:n AA <- an(a1, r, ii) SS <- sn(a1, r, ii) return(list(an = A, sn = S, output = data.frame(values = AA, sum = SS)))}

When you run the function, you will have the following output. Recall to have the sn and an functions loaded in the workspace.

asn()
$`an`[1] 16$sn[1] 31$output values sum1 1 12 2 33 4 74 8 155 16 31

You may have noticed that in the previous case it is equivalent to use the return function or not using it. However, consider the following example, where we want to check whether the parameters passed to the arguments are numbers or not. For this, if any of the parameters is not a number we will return a string, but if they are numbers the code will continue executing.

asn <- function(a1 = 1, r = 2, n = 5) { if(!is.numeric(c(a1, r, n))) return("The parameters must be numbers") A <- an(a1, r, n) S <- sn(a1, r, n) ii <- 1:n AA <- an(a1, r, ii) SS <- sn(a1, r, ii) return(list(an = A, sn = S, output = data.frame(values = AA, sum = SS)))}
asn("3")
"The parameters must be numbers"

If we have used the print function instead of return, when some parameter is not numeric, the text will be returned but also an error, since all the code will be executed.

asn <- function(a1 = 1, r = 2, n = 5) { if(!is.numeric(c(a1, r, n))) print("The parameters must be numbers") A <- an(a1, r, n) S <- sn(a1, r, n) ii <- 1:n AA <- an(a1, r, ii) SS <- sn(a1, r, ii) return(list(an = A, sn = S, output = data.frame(values = AA, sum = SS)))}
asn("3")
"The parameters must be numbers"Error in a1 * r^(n - 1) : non-numeric argument to binary operator

Local and global variables in R

In R it is not necessary to declare the variables used within a function. The rule called “lexicographic scope” is used to decide whether an object is local to a function or global. Consider, for instance, the following example:

fun <- function() { print(x)}x<- 1fun() # 1

The variable x is not defined within fun, so R will search for x within the “surrounding” scope and print its value. If x is used as the name of an object inside the function, the value of x in the global environment (outside the function) does not change.

x <- 1fun2 <- function() { x <- 2 print(x)}fun2() # 2x #1

To change the global value of a variable inside a function you can use the double assignment operator (<<-).

x <- 1y <- 3fun3 <- function() { x <- 2 y <<- 5 print(paste(x, y))}fun3() # 2 5x # 1 (the value hasn't changed)y # 5 (the value has changed)

Writing a function in R. Examples

In this section different examples of R functions are shown in order to illustrate the creation and use of R functions.

Example function 1: Letter of Spanish DNI

Let’s calculate the letter of the DNI from its corresponding number. The method used to obtain the letter (L) of the DNI consists of dividing the number by 23 and according to the remainder (R) obtained award the letter corresponding to the following table.

RLRLRLRL
0T7F14Z21K
1R8P15S22E
2W9D16Q
3A10X17V
4G11B18H
5M12N19L
6Y13J20C

The function will be like the following.

DNI <- function(number) { letters <- c("T", "R", "W", "A", "G", "M", "Y", "F", "P", "D", "X", "B", "N", "J", "Z", "S", "Q", "V", "H", "L", "C", "K", "E") letters <- letters[number %% 23 + 1] return(letters)}
DNI(50247828) # G

Example function 2: Throwing a die

The next function simulates n (by default n = 100) dice throws. The function returns the frequency table and the corresponding plot.

dice <- function(n = 100){ throws <- sample(1:6, n, rep = T) frequency <- table(throws)/n barplot(frequency, main = "") abline(h = 1/6, col = 'red', lwd = 2) return(frequency)}

Now you can see the simulation results executing the function.

par(mfcol = c(1, 3))dice(100)dice(500)dice(100000)
# 100 1 2 3 4 5 60.17 0.11 0.20 0.16 0.25 0.11# 500 1 2 3 4 5 60.144 0.158 0.148 0.178 0.164 0.208# 100000 1 2 3 4 5 60.16612 0.16630 0.16569 0.16791 0.16697 0.16701

Create functions in R (4)

As you can see, as we increase n we are closer to the theoretical value 1/6 = 0.1667.

Create functions in R (2024)

FAQs

How do you create a function in R *? ›

There are three key steps to creating a new function:
  1. You need to pick a name for the function. ...
  2. You list the inputs, or arguments, to the function inside function . ...
  3. You place the code you have developed in body of the function, a { block that immediately follows function(...) .

How to create a function with two arguments in R? ›

To create a function with two inputs, we just need to provide two different arguments inside function. For example, if we want to create a function to find the square of a+b then we can use x and y inside function.

How to get help for a function in R? ›

To access R's built-in help facility to get information on any function simply use the help() function.

How many arguments can an R function take? ›

In R programming, we can use as many arguments as we want and are separated by a comma. There is no limit on the number of arguments in a function in R.

How to make custom functions in R? ›

How do I create a custom function in R?
  1. Pick a name for your function, like add_three() .
  2. Use the function() command and put any inputs you need in parentheses, like function(x) .
  3. Use curly braces {} to start and end the part of the function that does the work.
Apr 5, 2024

What keyword is used for creating an R function? ›

In R functions are created using function keyword.

How to add 2 functions in R? ›

The lapply () and sapply () functions can be used for performing multiple functions on a list in R. This function is used in order to avoid the usage of loops in R. The difference between both the functions is the sapply () function does the same job as lapply () function but returns a vector.

What is the structure of a function in R? ›

A function has three parts: The formals() , the list of arguments that control how you call the function. The body() , the code inside the function. The environment() , the data structure that determines how the function finds the values associated with the names.

How to call a function in R programming? ›

R Functions
  1. Creating a Function. To create a function, use the function() keyword: Example. ...
  2. Call a Function. To call a function, use the function name followed by parenthesis, like my_function(): Example. ...
  3. Default Parameter Value. The following example shows how to use a default parameter value.

How do you create an IF function in R? ›

To run an if-then statement in R, we use the if() {} function. The function has two main elements, a logical test in the parentheses, and conditional code in curly braces. The code in the curly braces is conditional because it is only evaluated if the logical test contained in the parentheses is TRUE .

Why is R not finding a function? ›

6.1 Error: could not find function

This error usually occurs when a package has not been loaded into R via library . R does not know where to find the specified function.

How to use mathematical functions in R? ›

Built-in Functions in R
  1. print(). Displays an R object on the R console.
  2. min(), max(). Calculates the minimum and maximum of a numeric vector.
  3. sum(). Calculates the sum of a numeric vector.
  4. mean(). Calculates the mean of a numeric vector.
  5. range(). ...
  6. str(). ...
  7. ncol(). ...
  8. length().

What is the difference between an argument and a function in R? ›

An argument in R is the information passed into a function. In simpler terms, an argument is a value that is sent to a function when it is called. The parameter is the value enclosed in the parentheses whenever a function is created.

Can an R function have no arguments? ›

Calling a Function in R

In R, function arguments can be passed by position, by name (so-called named arguments), by mixing position-based and name-based matching, or by omitting the arguments at all. In the above example, x is equal to 3 and y – to 1, and not vice versa.

How do you pass arguments to a function in R? ›

How to pass arguments in R. We use arguments in R to pass the input value to a function. We have the option to pass a single argument or multiple arguments that are separated by commas. We pass the argument type inside the function() keyword in R while creating a function and argument values while calling the function.

How do I turn an R code into a function? ›

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.

How to write a function with if in R? ›

To run an if-then statement in R, we use the if() {} function. The function has two main elements, a logical test in the parentheses, and conditional code in curly braces. The code in the curly braces is conditional because it is only evaluated if the logical test contained in the parentheses is TRUE .

How do you create an input function in R? ›

You can create an input function from an R data frame using the input_fn() method. You can specify feature and response variables either explicitly or using the R formula interface. Note that input_fn functions provide several parameters for controlling how data is drawn from the input source.

How do you make a mean function in R? ›

At the heart of calculating averages in R lies the mean() function, a straightforward yet powerful tool. The basic syntax is mean(x, na. rm = FALSE) , where x represents the numeric vector or object, and na. rm denotes whether NA (missing) values should be removed before the calculation.

References

Top Articles
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 6449

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.