How to write a recursive function in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to write a recursive function in R?

How to write a recursive function in R?

This recipe helps you write a recursive function in R

0

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

Relevant Projects

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.