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

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Build a Multi Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

Langchain Project for Customer Support App in Python
In this LLM Project, you will learn how to enhance customer support interactions through Large Language Models (LLMs), enabling intelligent, context-aware responses. This Langchain project aims to seamlessly integrate LLM technology with databases, PDF knowledge bases, and audio processing agents to create a comprehensive customer support application.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

AWS MLOps Project for Gaussian Process Time Series Modeling
MLOps Project to Build and Deploy a Gaussian Process Time Series Model in Python on AWS

Build CNN Image Classification Models for Real Time Prediction
Image Classification Project to build a CNN model in Python that can classify images into social security cards, driving licenses, and other key identity information.

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.