Friday, November 27, 2020

R Functions

 A set of statements which are organized together to perform a specific task is known as a function. R provides a series of in-built functions, and it allows the user to create their own functions. Functions are used to perform tasks in the modular approach.

Functions are used to avoid repeating the same task and to reduce complexity. To understand and maintain our code, we logically break it into smaller parts using the function. A function should be

  1. Written to carry out a specified task.
  2. May or may not have arguments
  3. Contain a body in which our code is written.
  4. May or may not return one or more output values.

"An R function is created by using the keyword function." There is the following syntax of R function:


  1. func_name <- function(arg_1, arg_2, ...) {  
  2.    Function body   
  3. }  

Components of Functions

There are four components of function, which are as follows:

R Functions

Function Name

The function name is the actual name of the function. In R, the function is stored as an object with its name.

Arguments

In R, an argument is a placeholder. In function, arguments are optional means a function may or may not contain arguments, and these arguments can have default values also. We pass a value to the argument when a function is invoked.

Function Body

The function body contains a set of statements which defines what the function does.

Return value

It is the last expression in the function body which is to be evaluated.

Function Types

Similar to the other languages, R also has two types of function, i.e. Built-in Function and User-defined Function. In R, there are lots of built-in functions which we can directly call in the program without defining them. R also allows us to create our own functions.

R Functions

Built-in function

The functions which are already created or defined in the programming framework are known as built-in functions. User doesn't need to create these types of functions, and these functions are built into an application. End-users can access these functions by simply calling it. R have different types of built-in functions such as seq(), mean(), max(), and sum(x) etc.

  1. # Creating sequence of numbers from 32 to 46.  
  2. print(seq(32,46))  
  3.   
  4. # Finding the mean of numbers from 22 to 80.  
  5. print(mean(22:80))  
  6.   
  7. # Finding the sum of numbers from 41 to 70.  
  8. print(sum(41:70))  

Output:

R Functions

User-defined function

R allows us to create our own function in our program. A user defines a user-define function to fulfill the requirement of user. Once these functions are created, we can use these functions like in-built function.

  1. # Creating a function without an argument.  
  2. new.function <- function() {  
  3.    for(i in 1:5) {  
  4.       print(i^2)  
  5.    }  
  6. }     
  7.   
  8. new.function()  

Output:

R Functions

Function calling with an argument

We can easily call a function by passing an appropriate argument in the function. Let see an example to see how a function is called.

  1. # Creating a function to print squares of numbers in sequence.  
  2. new.function <- function(a) {  
  3.    for(i in 1:a) {  
  4.       b <- i^2  
  5.       print(b)  
  6.    }  
  7.   
  8. # Calling the function new.function supplying 10 as an argument.  
  9. new.function(10)  

Output:

R Functions

Function calling with no argument

In R, we can call a function without an argument in the following way

  1. # Creating a function to print squares of numbers in sequence.  
  2. new.function <- function() {  
  3.    for(i in 1:5) {  
  4.       a <- i^2  
  5.       print(a)  
  6.    }  
  7. }  
  8.   
  9. # Calling the function new.function with no argument.  
  10. new.function()  

Output:

R Functions

Function calling with Argument Values

We can supply the arguments to a function call in the same sequence as defined in the function or can supply in a different sequence but assigned them to the names of the arguments.

  1. # Creating a function with arguments.  
  2. new.function <- function(x,y,z) {  
  3.    result <- x * y + z  
  4.    print(result)  
  5. }  
  6.   
  7. # Calling the function by position of arguments.  
  8. new.function(11,13,9)  
  9.   
  10. # Calling the function by names of the arguments.  
  11. new.function(x = 2, y = 5, z = 3)  

Output:

R Functions

Function calling with default arguments

To get the default result, we assign the value to the arguments in the function definition, and then we call the function without supplying argument. If we pass any argument in the function call, then it will get replaced with the default value of the argument in the function definition.

  1. # Creating a function with arguments.  
  2. new.function <- function(x = 11, y = 24) {  
  3.    result <- x * y  
  4.    print(result)  
  5. }  
  6.   
  7. # Calling the function without giving any argument.  
  8. new.function()  
  9.   
  10. # Calling the function with giving new values of the argument.  
  11. new.function(4,6)  

Output:

R Functions

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...