Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Imagine you have a machine that can make different types of food. You just need to tell the name of the food, and it will make it up to you. In R, a function is like the machine. It can do some specific tasks for you, and arguments are similar to the choices we provide to the function.
Don't worry if you can't understand it now. In this blog, we will discuss function arguments in R, the types of function arguments, and how to pass them with examples to make them clearer for you.
What is R?
In the world of data science, we use R mostly for data cleaning or visualisation. It creates stunning visuals that help us analyse the data easily and quickly.
It is similar to Python programming language. It is an open-source programming language that is user-friendly and which offers different packages and libraries that help any user analyse complex datasets.
What are Functions in R?
Functions are the blocks of organised code that will run when it is called. Let us try to understand with an example of creating and using a function in R.
We will create a simple function that prints “Hello, Ninjas!” when it is called.
In R, we use the keyword function() to create a new function. There are different types of functions in R. Let us understand them individually.
User Defined Functions
In R programming, user-defined functions represent those unique functions made by individual users instead of being included within R's primary library or extensions. When we use user defined functions, we generally need to use the argument that we specified while creating the function.
For example, if we make a function to calculate the product of all the numbers in a vector, it will be called a user defined function.
product <- function(vector) {
result <- 1
for (num in vector) {
result <- result * num
}
return(result)
}
my_vector <- c(2, 3, 4)
product_result <- product(my_vector)
print(paste("The product of the elements in the vector is:", product_result))
Output
Explanation:
In this example, we have used a vector as an argument to evaluate the product of the elements in a vector.
In-Built Functions
In-Built functions are present in the library or extensions of R. We need to call the function along with the arguments that the function may require. Some examples of in-built functions include sum(), mean(), sqrt() and sort().
For example, the sum() function is used to calculate the sum of the elements of a vector. It requires the vector for which the sum is needed.
vector <- c(1, 2, 3)
ans <- sum(vector)
print(paste("The sum of the elements in the vector is:", ans))
Output
Rbind and Cbind functions
In R, rbind and cbind are the two functions that combine objects by specific orders. The rbind function combines all the vectors and dataframes by rows. Similarly, the cbind function combines all the vectors and dataframes by columns. They both require the vectors name which we need to combine.
We will take an example to make both functions clearer to you.
vector1 <- c(1, 2, 3)
vector2 <- c(4, 5, 6)
# using cbind() function
cbindvector <- cbind(vector1, vector2)
print(cbindvector)
Output
# using rbind() function
rbindvector <- rbind(vector1, vector2)
print(rbindvector)
Output
How to pass arguments in R
We use arguments in R to pass the input value to a function. We have the option to pass a single argument or multiple arguments that are separated by commas. We pass the argument type inside the function() keyword in R while creating a function and argument values while calling the function.
Syntax
#while creating a function
my_function<-function(arg1, arg2, arg3, ...)
Example of creating a function with multiple arguments:
calculate_average <- function(num1, num2, num3) {
average <- (num1 + num2 + num3) / 3
return(average)
}
#while calling a function
my_function(arg1 value, arg2 value, arg3 value, …)
Calling the above created function:
# Example call to the calculate_average function
result <- calculate_average(5, 8, 12)
print(result)
Types of Arguments for Functions in R
Arguments are the expressions we use when creating or calling a function. They provide input data to the function, and the function uses its values to show us the result. There are several types of arguments in R. We will look at them one by one and understand them with the help of examples.
Using Dots Argument
In R, the dots argument helps us to define any number of arguments for a function. The dots argument is mostly used to create a wrapper function that can accept various arguments and forward the argument to other functions. We use the eclipse notation “...” to denote the dots argument in R.
For example, if we have a long list of arguments that is to be provided to a function, we use the dots argument in this case.
Let us take an example to make it more clear for you.
In this example, we have used the dots argument to print a list of all the arguments one by one.
Using Functions as Arguments
In R, we can also use a function as the argument to other functions. This concept of using functions as arguments is used in dynamic programming, where we calculate a value of a function using a smaller function whose value is previously calculated.
For example, we can calculate the factorial of a number by using a smaller number whose factorial is known to us.
factorial <- function(n) {
if (n == 1) {
print(paste("Base case reached for factorial(", n, ")"))
return(1)
}
else {
recursive_result <- factorial(n - 1)
result <- n * recursive_result
print(paste("Calculating factorial(", n, ")"))
print(paste("Recursive call: factorial(", n, ") =", n, "* factorial(", n - 1, ") =", result))
return(result)
}
}
factorial_5 <- factorial(5)
print(factorial_5)
Output
Explanation:
In this example, we have calculated the factorial of 5 by using the factorial of 4 as an argument. We use the function again and again for the smaller number until we have reached the base case that is factorial of 1.
Adding a Default Value as an Argument
We can also use a default value as an argument in R so that any time we do not provide the argument while calling the function, it won't show an error. Rather, it would show us the result using the default value as the argument. We will take an example to make it more clear for you.
Function for greeting a person:
greet <- function(name = "Anonymous person") {
message <- paste("Hello,", name)
print(message)
}
# Calling the function without an argument
greet()
# Calling the function with an argument
greet("Aditya")
Output
Explanation:
In this example, we used the default value of the argument as Anonymous person. When we used the function without any argument, it printed the default value.
Using Anonymous Functions as an Argument
Anonymous functions are those functions that do not have any name and are often called lambda functions. Anonymous functions are small, one-time-use functions that are used to simplify and shorten the code.
For example, we can create an anonymous function to calculate the square of an element in a vector. In this example, we used the Anonymous function as an argument to calculate the square of every element of the vector.
Let us look at the example below to clarify for you.
In this example, we used the sapply() function to apply an anonymous function that calculates the square of a number to all the elements of the vector and saves the results in a new vector named squared.
Frequently Asked Questions
What is R programming language?
R is one of the most used programming languages for big calculations and analysing data.
What are the functions in R?
Functions are the blocks of organised code that will run when it is called.
What are the arguments in R?
Arguments are the expressions that we use when we create or call a function.
What are the different types of functions in R?
There are different types of functions in R, such as user defined, in-built, cbind, rbind functions, lambda functions and higher order functions.
What are the different types of arguments in R?
There are several types of arguments in R, such as dots argument, functions as an argument, lambda functions as arguments and default values as arguments.
Conclusion
This article discusses the topic of function arguments in the R programming language. In this blog, we have discussed function arguments in R along with the types of function arguments and how to pass them with examples.
We hope this blog has helped you enhance your knowledge about function arguments in the R programming language. If you want to learn more, then check out our articles.
But suppose you have just started your learning process and are looking for questions from tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problems, interview experiences, and interview bundles for placement preparations.
However, you may consider our paid courses to give your career an edge over others!