Functions in R

Functions are useful when you want to perform a certain task multiple times. A function accepts input arguments and produces the output by executing valid R commands that are inside the function. In R Programming Language when you are creating a function the function name and the file in which you are creating the function need not be the same and you can have one or more functions in R.

Creating a Function in R

Functions are created in R by using the command function(). The general structure of the function file is as follows:

Syntax:
function_name = function(arg_1, arg_2, …)
{
Function body
}
 
The various components/parts of a function are:
Function name: It is the actual name of the function. It is stored in R environment as an object with this name.
Arguments: An argument is a placeholder. Whenever a function is invoked, a value if passed to the argument.They are optional; that is, a function may contain no arguments. Also arguments can have default values.
Function Body: It contains all the set of statements that defines what actually the function does.
Return Values: It is the values that function returns after the successful execution of the tasks.In more general,it is the last expression in the function body to be evaluated.

Types of Function in R Language
 
Built-in Function: Built-in functions in R are pre-defined functions that are available in R programming languages to perform common tasks or operations.

User-defined Function: R language allow us to write our own function.
 
Built-in Function in R Programming Language

Here we will use built-in functions like sum(), max() and min().
 
# Find sum of numbers 4 to 6.
print(sum(4:6))

# Find max of numbers 4 and 6.
print(max(4:6))

# Find min of numbers 4 and 6.
print(min(4:6))
output:
[1] 15
[1] 6
[1] 4 

User-defined Functions in R Programming Language

R provides built-in functions like print(), cat(), etc. but we can also create our own functions. These functions are called user-defined functions.
Example
# Function without argument
# create a function cube
# without an argument
cube <- function()
{
for(i in 1:10)
{
print(paste0(i,"^3=",i^3))
}
}

# calling function cube without an argument
cube()
output:
[1] "1^3=1"
[1] "2^3=8"
[1] "3^3=27"
[1] "4^3=64"
[1] "5^3=125"
[1] "6^3=216"
[1] "7^3=343"
[1] "8^3=512"
[1] "9^3=729"
[1] "10^3=1000"

# A simple R function to check
# whether x is even or odd

evenOdd = function(x){
if(x %% 2 == 0)
    return("even")
else
    return("odd")
}

print(evenOdd(4))
print(evenOdd(3))

output:
[1] "even"
[1] "odd"

R Function Example – Single Input Single Output

Now create a function in R that will take a single input and gives us a single output.

Following is an example to create a function that calculates the area of a circle which takes in the arguments the radius. So, to create a function, name the function as “areaOfCircle” and the arguments that are needed to be passed are the “radius” of the circle. 

example:
# A simple R function to calculate
# area of a circle

areaOfCircle = function(radius){
area = pi*radius^2
return(area)
}

print(areaOfCircle(2))

output:
[1] 12.56637


R Function Example – Multiple Input Multiple Output

Now create a function in R Language that will take multiple inputs and gives us multiple outputs using a list.

The functions in R Language take multiple input objects but returned only one object as output, this is, however, not a limitation because you can create lists of all the outputs which you want to create and once the list is created you can access them into the elements of the list and get the answers which you want.

Let us consider this example to create a function “Rectangle” which takes “length” and “width” of the rectangle and returns area and perimeter of that rectangle. Since R Language can return only one object. Hence, create one object which is a list that contains “area” and “perimeter” and return the list. 
 
Example:
# A simple R function to calculate
# area and perimeter of a rectangle

Rectangle = function(length, width){
area = length * width
perimeter = 2 * (length + width)

# create an object called result which is
# a list of area and perimeter
result = list("Area" = area, "Perimeter" = perimeter)
return(result)
}

resultList = Rectangle(2, 3)
print(resultList["Area"])
print(resultList["Perimeter"])
 
output:
$Area
[1] 6

$Perimeter
[1] 10
 

Inline Functions in R Programming Language


Sometimes creating an R script file, loading it, executing it is a lot of work when you want to just create a very small function. So, what we can do in this kind of situation is an inline function.

To create an inline function you have to use the function command with the argument x and then the expression of the function.

Example 
# A simple R program to
# demonstrate the inline function


f = function(x) x^2*4+x/3

print(f(4))
print(f(-2))
print(f(0))
 
output:
[1] 65.33333
[1] 15.33333
[1] 0
 
Passing Arguments to Functions in R Programming Language

There are several ways you can pass the arguments to the function: 
 
Case 1: Generally in R, the arguments are passed to the function in the same order as in the function definition.
Case 2: If you do not want to follow any order what you can do is you can pass the arguments using the names of the arguments in any order.
Case 3: If the arguments are not passed the default values are used to execute the function.
 
Example:
# A simple R program to demonstrate
# passing arguments to a function

Rectangle = function(length=5, width=4){
area = length * width
return(area)
}

# Case 1:
print(Rectangle(2, 3))

# Case 2:
print(Rectangle(width = 8, length = 4))

# Case 3:
print(Rectangle())
 
output:
[1] 6
[1] 32
[1] 20
 
Variable Length Argument

In R, it is often convenient to accept a variable number of arguments passed to the function. To do this, you specify an ellipsis (...) in the arguments when defining a function. It accepts variable number of arguments, in the sense that you do not know beforehand how many arguments can be passed to your function by the user.
For example, below function prints the first argument and then passes all the other arguments to the summary() function.

myfunc <- function(x,...) {print(x); summary(...)}
v <- 1:10
myfunc("Summary of v:",v)
output:
[1] "Summary of v:"
Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
   1.00    3.25    5.50    5.50    7.75   10.00 

You can also directly refer to the arguments within the argument list (...) through the variables ..1, ..2, to etc.For example, ..1 refers to the first argument, ..2 refers to the second, and so on.

example;
myfunc <- function(...) {cat(..1, ..2)}
myfunc("Hello", "World!") 
output:
Hello World!

It is also possible to read the arguments from the argument list by converting the object (...) to a list within the function body.

For example, below function simply sums all its arguments:
addAll <- function(...) {
  args <- list(...)
  s=0
  for (a in args){
    s <- s + a  
  } 
  print(s)
}
addAll(1,2,3,4,5)

output:
[1] 15

Lazy Evaluations of Functions in R Programming Language

In R the functions are executed in a lazy fashion. When we say lazy what it means is if some arguments are missing the function is still executed as long as the execution does not involve those arguments.

Example

In the function “Cylinder” given below. There are defined three-argument “diameter”, “length” and “radius” in the function and the volume calculation does not involve this argument “radius” in this calculation. Now, when you pass this argument “diameter” and “length” even though you are not passing this “radius” the function will still execute because this radius is not used in the calculations inside the function.
Let’s illustrate this in an R code given below:
 
# A simple R program to demonstrate
# Lazy evaluations of functions

Cylinder = function(diameter, length, radius ){
volume = pi*diameter^2*length/4
return(volume)
}

# This'll execute because this
# radius is not used in the
# calculations inside the function.
print(Cylinder(5, 10))
 
output
[1] 196.3495
 
Nested Functions
 
Write a Function Inside Another Function.Let's create a nested function by writing a function inside another function. 
# define a function to compute power
power <- function(a) {
    exponent <- function(b) {
        return (a^b)
    }
    return (exponent)
}

# call nested function
result <- power(2)
print(result(3))
output:
[1] 8


Infix Functions

Infix functions are those functions in which the function name comes in between its arguments, and hence have two arguments. R comes with a number of built-in infix operators such as :, ::, :::, $, @, ^, *, /, +, -, >, >=, <, <=, ==, !=, !, &, &&, |, ||, ~, <-, and <<-. One can create his own infix functions that start and end with %. The name of an infix function is more flexible as it can contain any sequence of characters except %. There are some predefined infix operators in R programming.
OperatorsDescription
%%Remainder operator
%/%Integer Division
%*%Matrix multiplication
%o%Outer Product
%x%Kronecker product
%in%Matching Operator
Example: Create a two argument function that gives greater of two numbers and bind it to a name that starts and ends with %.

 


# R program to illustrate
# Infix function
'%Greater%' <- function(a, b)
{
if(a > b) print(a)
else if(b > a) print(b)
else print("equal")
}

5 %Greater% 7
23 %Greater% 7
7 %Greater% 7

output
[1] 7
[1]23
[1] "equal"





Important Functions



 Mathematical Functions 

abs()

calculates a number’s absolute value.

sqrt()

calculates a number’s square root. 

    

round()

rounds a number to the nearest integer.

exp()

calculates a number’s exponential value

log()

which calculates a number’s natural logarithm.

f. cos(), sin(), and tan()

calculates a number’s cosine, sine, and tang.
Statistical Functions 

mean()

 A vector’s arithmetic mean is determined by the mean() function. 

   median()

 A vector’s median value is determined by the median() function.

cor()

calculates the correlation between two vectors.

var()

 calculates the variance of a vector and calculates the standard deviation of a vector.
Data Manipulation Functions 

unique()

 returns the unique values in a vector.

subset()

subsets a data frame based on conditions.

    aggregate()

 groups data according to a grouping variable.

order()

uses ascending or descending order to sort a vector.
File Input/Output Functions 

read.csv()

reads information from a CSV file. 

file.csv()

publishes information to a read CSV file.

file. table()

reads information from a tabular.

file.table()

 creates a tabular file with data.
 




Comments

Popular posts from this blog

Programming in R - Dr Binu V P

Introduction

R Data Types