How to write and run a function in R

In this recipe, we will learn how to write user-defined functions in R. We will also learn how to call these functions with and without parameters.

How to write and run a function in R

In this tutorial, you will learn –
• How to define a function in R
• How to call a function in R

Learn to Implement Customer Churn Prediction Using Machine Learning in Python 

We use “function()” to define a function in R. The syntax for defining an R function is as follows –
function_name <- function(arg1, arg2, ...) {
function body
}
As we can see, the different components of a function include the function’s name, its arguments, function body, and a return value. In R, we can define our functions. They are tailored to the needs of the user and, once constructed, can be utilized in the same way as built-in functions. Here's an example of how to write and use a function.


Code:
#defining a function to calculate factorial
fact <- function(num){
facto = 1
for(i in 1:num) {
facto = facto * i
}
print(paste(" The factorial of ", num ,"is", facto ))
}

Now that we have defined the function, let us call the function with an argument.

Code:
#calling the function
fact(7)

 
Output:
[1] " The factorial of  7 is 5040"

We can even call the function without an argument as follows –


Code:
#defining a function to calculate factorial of 9
fact <- function(){
facto = 1
for(i in 1:9) {
facto = facto * i
}
print(paste(" The factorial of 9 is", facto ))
}

#calling the function
fact()

 
Output:
[1] " The factorial of 9 is 362880"

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

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 a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

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.

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.

AWS MLOps Project to Deploy a Classification Model [Banking]
In this AWS MLOps project, you will learn how to deploy a classification model using Flask on AWS.

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.