How to Return Multiple Values from Function in R


A function in R can be used to perform some specific task.

When creating your own custom function in R, you may want to return multiple values.

You can use the following basic syntax to do so:

#define function
my_function <- function(x) {
   first <- x*2
   second <- x/2
   output <- list(first, second)
   return(output)
}

#use function with input value of 10
my_results <- my_function(10)

#print results of function
for (i in my_results) {
   print(i)
}

Here is what this syntax does:

  • First, it creates a function named my_function that returns two values: The input value multiplied by 2 and the input value divided by 2
  • Within the function, we use list() to store two values as output
  • When printing the results of the function, we use a for() loop to print all output values

Using this method, we are able to return multiple values from one function.

The following example shows how to use this syntax in practice.

Related: How to Exit a Function in R

Example: How to Return Multiple Values from Function in R

Suppose that we would like to create a function in R that accepts one value as input and then returns the following two values:

  • The input value multiplied by 2
  • The input value divided by 2

We can use the following syntax to define this function and then print the results from it:

#define function
my_function <- function(x) {
   first <- x*2
   second <- x/2
   output <- list(first, second)
   return(output)
}

#use function with input value of 10
my_results <- my_function(10)

#print results of function
for (i in my_results) {
   print(i)
}

[1] 20
[1] 5

This function returns the following two values:

  • 10 multiplied by 2 = 20
  • 10 divided by 2 = 5

Note that we can also use double brackets with a specific number to only return the nth return value from the function.

For example, we can use the following syntax to define our function and then only return the first value from the output:

#define function
my_function <- function(x) {
   first <- x*2
   second <- x/2
   output <- list(first, second)
   return(output)
}

#use function with input value of 10 and return only first output
my_function(10)[[1]]

[1] 20

This function returns the following value:

  • 10 multiplied by 2 = 20

We could also use the following syntax to define our function and then only return the second value from the output:

#define function
my_function <- function(x) {
   first <- x*2
   second <- x/2
   output <- list(first, second)
   return(output)
}

#use function with input value of 10 and return only second output
my_function(10)[[2]]

[1] 5

This function returns the following value:

  • 10 divided by 2 = 5

By specifying my_function(10)[[n]] we are able to provide an input value of 10 to the function and then return the nth value from the output list.

Note: In each of these examples we used a function that returned two values but you can use similar syntax to create a function that returns as many values as you would like.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Write a Repeat Loop in R
How to Append Values to a Vector Using a Loop in R
How to Create a Nested For Loop in R
How to Use While Loops in R

Leave a Reply

Your email address will not be published. Required fields are marked *