Recursion, in the simplest terms, is a type of looping technique. It exploits the basic working of functions in R. Recursion is when the function calls itself. This forms a loop, where every time the function is called, it calls itself again and again and this technique is known as recursion. Since the loops increase the memory we use the recursion. The recursive function uses the concept of recursion to perform iterative tasks they call themselves, again and again, which acts as a loop. These kinds of functions need a stopping condition so that they can stop looping continuously.

Recursive functions call themselves. They break down the problem into smaller components. The function() calls itself within the original function() on each of the smaller components. After this, the results will be put together to solve the original problem.

**Example: Factorial using Recursion in R**

`rec_fac <` `-` `function(x){ ` ` ` `if` `(x` `=` `=` `0` `|| x` `=` `=` `1` `) ` ` ` `{ ` ` ` `return` `(` `1` `) ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `return` `(x` `*` `rec_fac(x` `-` `1` `)) ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

**Output:**

[1] 120

Here, rec_fac(5) calls rec_fac(4), which then calls rec_fac(3), and so on until the input argument x, has reached 1. The function returns 1 and is destroyed. The return value is multiplied with argument value and returned. This process continues until the first function call returns its output, giving us the final result.

**Example: Sum of Series Using Recursion**

Recursion in R is most useful for finding the sum of self-repeating series. In this example, we will find the sum of squares of a given series of numbers.

Sum = 1^{2}+2^{2}+…+N^{2}

**Example:**

`sum_series <` `-` `function(vec){ ` ` ` `if` `(length(vec)<` `=` `1` `) ` ` ` `{ ` ` ` `return` `(vec^` `2` `) ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `return` `(vec[` `1` `]^` `2` `+` `sum_series(vec[` `-` `1` `])) ` ` ` `} ` `} ` `series <` `-` `c(` `1` `:` `10` `) ` `sum_series(series) ` |

*chevron_right*

*filter_none*

**Output:**

[1] 385

#### Key Features of R Recursion

- The use of recursion, often, makes the code shorter and it also looks clean.
- It is a simple solution for a few cases.
- It expresses in a function that calls itself.

#### Applications of Recursion in R

- Recursive functions are used in many efficient programming techniques like dynamic programming language(DSL) or divide and conquer algorithms.
- In dynamic programming, for both top-down as well as bottom-up approaches, recursion is vital for performance.
- In divide and conquer algorithms, we divide a problem into smaller sub-problems that are easier to solve. The output is then built back up to the top. Recursion has a similar process, which is why it is used to implement such algorithms.
- In its essence, recursion is the process of breaking down a problem into many smaller problems, these smaller problems are further broken down until the problem left is trivial. The solution is then built back up piece by piece.

## Recommended Posts:

- Functions in R Programming
- Types of Functions in R Programming
- Conversion Functions in R Programming
- Poisson Functions in R Programming
- Poisson Functions in R Programming
- Logarithmic and Power Functions in R Programming
- Performing Logarithmic Computations in R Programming - log(), log10(), log1p(), and log2() Functions
- Gamma Distribution in R Programming - dgamma(), pgamma(), qgamma(), and rgamma() Functions
- Exponential Distribution in R Programming - dexp(), pexp(), qexp(), and rexp() Functions
- Compute Beta Distribution in R Programming - dbeta(), pbeta(), qbeta(), and rbeta() Functions
- Applying User-defined Functions on Factor Levels of Dataset in R Programming - by() Function
- Compute the Parallel Minima and Maxima between Vectors in R Programming - pmin() and pmax() Functions
- Get Summary of Results produced by Functions in R Programming - summary() Function
- Inverse functions and composition of functions
- Perl | Recursive Subroutines
- Anonymous recursive function in PHP
- Recursive Anonymous Function in Golang
- ES6 | Functions
- Functions in Python
- Functions of Operating System

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.