Flat 10% & upto 50% off + Free additional Courses. Hurry up!
×
UPTO
50%
OFF!

# Functions

A function is a group of statements sequenced to form a particular task. R has a large number of in-built functions and the user can create their own functions.

An R function is produced via keyword function. The basic syntax of R function is:

function_name  <- function(arg1, arg2,…){

Function body

}

Function Components

The different components of a function are:

• Function Name: This is stored as an object with this name.
• Arguments: An argument is a reserved position. These are optional; A value is passed to an argument when a function is called.
• Function Body: Function body includes the statements that define what the function does.
• Function Body: The function body includes a collection of the statements that defines what the function does.
• Return value: It is the expression at the end of function body that will be evaluated.

R has many in-built functions which can directly be called in the program without defining them first. We have an opportunity to create our own functions referred to as user-defined functions.

Built-in Function

Some examples of in-built functions are seq(), max(), sum(x), mean(), paste(), etc,. These are directly called by user written functions.

Example:

#Printing a sequence of numbers from 12 to 16

print(seq(12,16))

#Printing mean of numbers from 12 to 16

print(mean(12:16))

Output:

[1] 12 13 14 15 16

[1] 14

User- defined Functions

We can create user defined functions and once created can be used like built-in functions.

Example:

#Printing squares of numbers in sequence

new.function <<- function(b){

for (i in 2:b)

{

c<- i^2

print(c)

}

}

Calling a Function

Example:

# Printing squares of numbers in sequence

new.function <- function (c)

{

for(i in 1:c) {

d <- i^2

print(d)

}

}

#Call the new.function by passing 4 as an argument

new.function(6)

Output:

[1] 1

[2] 4

[1] 9

[1] 16

Call a Function without an argument

Example:

new.function <<- function()

{

for(x in 1:4) {

print(x^2)

}

}

#call the new.function without passing an argument

new.function()

Output:

[1] 1

[1] 4

[1] 9

[1] 16

Calling a Function with Argument Values (by name and by position)

Arguments can be passed in the same sequence as defined in the function or can be supplied in different sequence by assigning to the names of the sequence

Example:

#creating a function with arguments

new.function <- function(x,y,z)

{

f<- x * y+z

print(f)

}

#call the function by names of the argument

new.function(a=3, b=4, c=2)

#call the function by position of arguments

new.function(2,3,4)

Output:

[1] 14

[1] 10

Calling a Function with Default Argument

Arguments can be declared at the function definition to get default result. Default argument functions can be invoked by supplying new values of the argument to get a non-default result.

Example:

#Creating a function with arguments

new.function <- function (x = 2, y=3)

{

res<- x*y

print(res)

}

#Calling the function without giving any argument

new.function()

#Calling the function by passing new values to the argument

new.function(4,5)

Output:

[1] 6

[1] 20

### "0 Responses on Functions"

Training in Cities: Bangalore, Hyderabad, Chennai, Delhi, Kolkata, UK, London, Chicago, San Francisco, Dallas, Washington, New York, Orlando, Boston

100% Secure Payments. All major credit & debit cards accepted Or Pay by Paypal.

## Sales Offer

• To avail this offer, enroll before 27th March 2017.
• This offer cannot be combined with any other offer.
• This offer is valid on selected courses only.
• Please use coupon codes mentioned below to avail the offer