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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Tensorflow Transfer Learning Model for Image Classification
Image Classification Project - Build an Image Classification Model on a Dataset of T-Shirt Images for Binary Classification

Build a Langchain Streamlit Chatbot for EDA using LLMs
In this LLM project, you will build a Streamlit Chatbot integrated with Langchain technology for natural language interactions with a SQL database, facilitating real-time visualization and insightful insights, streamlining data exploration and analysis.

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

Deploy Transformer BART Model for Text summarization on GCP
Learn to Deploy a Machine Learning Model for the Abstractive Text Summarization on Google Cloud Platform (GCP)

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

Build Time Series Models for Gaussian Processes in Python
Time Series Project - A hands-on approach to Gaussian Processes for Time Series Modelling in Python

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

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

Microsoft Fabric Project to Build a Financial Reporting Agent
In this Microsoft Fabric project, you'll build a financial reporting agent that simplifies data management, automates analysis, and delivers real-time dashboards for wealth advisors and their clients.