R – Recursive Functions

The R Programming language introduced a new technique called Recursion for elegant and straightforward coding. Recursive functions in R means a function calling itself. To understand the R recursive functions programming, let us consider a well know, yet simple example called factorial.

We can calculate the factorial of any given number using the formula below.

n! = (n) * (n-1) * (n-2) * ….. * 1

It means, 6! = 6 * 5 * 4 * 3 * 2 * 1

In R programming, We can achieve the same using For Loop, While Loop, etc. But if you observe the above pattern, it’s behavior is repetitive, which means recursive. So instead of writing the loops (costly), we can write the recursive functions R Programming.

# Recursive Functions in R Example
Number.factorial <- function(number)
{
  if(number == 0 || number == 1) {
    return (1)
  } else {
    return (number *  Number.factorial(number - 1))
  }
}
Sum.Series(6)

OUTPUT

If we pass 0 or 1 as the Number argument value, then the function returns 1 else it returns below statement

(number *  Number.factorial(number - 1))

Let us calculate for 6!

6! = number *  Number.factorial(number - 1)

   = 6 * Number.factorial (6 -1) 

   = 6 * Number.factorial (5) # Recursively calling the above function

   = 6 * 5 * Number.factorial (5 -1)

   = 6 * 5 * Number.factorial (4) # Recursively calling the above function

   = 6 * 5 * 4 * Number.factorial (4 -1)

   = 6 * 5 * 4 * Number.factorial (3) # Recursively calling the above function

   = 6 * 5 * 4 * 3 * Number.factorial (3 -1)

   = 6 * 5 * 4 * 3 * Number.factorial (2) # Recursively calling the above function

   = 6 * 5 * 4 * 3 * 2 * Number.factorial (2 -1)

   = 6 * 5 * 4 * 3 * 2 * Number.factorial (1) # Recursively calling the above function

   = 6 * 5 * 4 * 3 * 2 * 1

   = 720

In this article, we show how to write a simple program using Recursive Functions in R Programming with a practical example.

Use Recursive Functions in R to Find Sum of Series 1²+2²+3²+…..+n²

In this example, we show how to write an R program to find the Sum of Series 1²+2²+3²+…..+n² using the Recursive Functions in R Programming.

# Recursive Functions in R Example
Sum.Series <- function(number)
{
  if(number == 0) {
    return (0)
  } else {
    return ((number * number ) + Sum.Series(number - 1))
  }
}
Sum.Series(5)

OUTPUT

ANALYSIS

Let me show you the step by step execution process of R recursive function

Function Definition

Within the Sum.Series (number) function,

If the user entered Number is 0, then the function returns 0 else it returns the following

(number * number ) + Sum.Series(number - 1)

Let us divide the above expression for better understanding

  • (number * number) = Multiplying the number
  • Sum.Series(number – 1) = Calling the same function with decremented value (1 number minus)

From the above screenshot, you can see the User entered value is 6:

Recursion 1

number = 6 which is Greater than 0 so,

  • (number * number ) + Sum.Series(number – 1)
  • (6 * 6) + Sum.Series(6 – 1)
  • 36 + Sum.Series(5)

Recursion 2

number becomes 5, which is Greater than 0 so,

  • (number * number ) + Sum.Series(number – 1)
  • (5 * 5) + Sum.Series(5 – 1)
  • 25 + Sum.Series(4)

Total will be 36 + 25 = 61

Recursion 3

number become 4, which is Greater than 0 so,

  • (number * number ) + Sum.Series(number – 1)
  • (4 * 4) + Sum.Series(4 – 1)
  • 16 + Sum.Series(3)

Total is 36 + 25 + 16 = 77

Recursion 4

number becomes 3, which is Greater than 0 so,

  • (number * number ) + Sum.Series(number – 1)
  • (3 * 3) + Sum.Series(3 – 1)
  • 9 + Sum.Series(2)

Total = 36 + 25 + 16 + 9 = 86

Recursion 5

number becomes 2, which is Greater than 0 so,

  • (number * number ) + Sum.Series(number – 1)
  • (2 * 2) + Sum.Series(2 – 1)
  • 4 + Sum.Series(1)

Total will be 36 + 25 + 16 + 9 + 4 = 90

Recursion 6

number becomes 1, which is Greater than 0 so,

  • (number * number ) + Sum.Series(number – 1)
  • (1 * 1) + Sum.Series(1 – 1)
  • 1 + Sum.Series(0)

Total = 36 + 25 + 16 + 9 + 4 + 1 = 91

Recursion 7

number becomes 0, which means First if condition is True, so it exits from the function.

Final Output is 91

NOTE: We must use some conditions to exit from the recursive function calling in R. If you forgot the condition, then the function executes countless times (similar to Infinity Loop).

Follow Us On