How to write a recursive function in R?

This recipe helps you write a recursive function in R

Recipe Objective

Recursion is a type of looping mechanism which exploits the working of functions in R. In R, recursion occurs when the function calls itself which results in a formation of loop. ​

Functions which uses the concept of rescursion to perform iterative tasks are known as Recursive functions.

Loops are an important feature in R-language but it increases the memory requirements of a program. Thus, recursive function is a better alternative than using a loop in many cases as it frees up memory used after every iteration. ​

In this recipe, we will learn how to write a recursive function in R by using an example of factorial using recursion in R. ​

Example: Factorial using Resursive function in R

Factorial of a number is calculated by the given formula: n! = (n)(n-1)(n-2)(n-3)....1

Steps to be followed:

  1. Creation of a recursive function using if..else statement
  2. Calling the function and getting an output

# 1. creation of a recursive function rec_func_factorial = function(n){ if (n == 0 || n == 1){ return (1) } else { return(n*rec_func_factorial(n-1)) } } # 2. calling the function #calculating the factorial of 3 rec_func_factorial(3)

6

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

Predictive Analytics Project for Working Capital Optimization
In this Predictive Analytics Project, you will build a model to accurately forecast the timing of customer and supplier payments for optimizing working capital.

Loan Eligibility Prediction Project using Machine learning on GCP
Loan Eligibility Prediction Project - Use SQL and Python to build a predictive model on GCP to determine whether an application requesting loan is eligible or not.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.