0% found this document useful (0 votes)
3 views

R unit 2

The document provides an overview of control statements in R programming, including loops (for, while, repeat) and conditional statements (if, if-else, ifelse). It explains the syntax and usage of these structures with examples, demonstrating how to iterate over sequences, vectors, lists, matrices, and data frames. Additionally, it introduces the 'Apply Family' functions for efficient data manipulation and discusses reserved words in R.

Uploaded by

vefole2166
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

R unit 2

The document provides an overview of control statements in R programming, including loops (for, while, repeat) and conditional statements (if, if-else, ifelse). It explains the syntax and usage of these structures with examples, demonstrating how to iterate over sequences, vectors, lists, matrices, and data frames. Additionally, it introduces the 'Apply Family' functions for efficient data manipulation and discusses reserved words in R.

Uploaded by

vefole2166
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Statistical Computing & R Programming

Unit 2
R Programming Structures
Control Statements
Control statements are expressions used to control the execution and flow of the
program based on the conditions provided in the statements. These structures are
used to make a decision after assessing the variable.
R provides two types of control statements:
1. Loops
• for loop
• while loop
• repeat loop
2. Conditional Statement
• if() statement
• if-else statement
• if-else-if statement
• nested if-else statement
• ifelse() function

Loops
There are three types of loops in R programming:
1. for loop
2. while loop
3. repeat loop

For Loop
• For R loop is commonly used to iterate over items of a sequence.
• It is an entry-controlled loop, in this loop, the test condition is tested first, then the
body of the loop is executed, the loop body would not be executed if the test
condition is false.
Syntax:
for (value in sequence)
{
statement
}

1. Flow Diagram:

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 1
Statistical Computing & R Programming

Example 1: Program to display numbers from 1 to 5 using for loop in R.

# R program to demonstrate the use of for loop


for (valin1: 5)
{
# statement
print(val)
}

Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

In this program for loop has iterated over a sequence of numbers from 1 to 5 displaying
each element of the sequence in every iteration.

Example 2: For loop with vector


# Program to display days of the week.
# Assigning strings to the vector

week <- c('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday')


# Using for loop to iterate over each string in the vector
for (day inweek)
{
# Displaying each string in the vector
print(day)
}

Output:
[1] "Sunday"
[1] "Monday"
[1] "Tuesday"
[1] "Wednesday"
[1] "Thursday"
[1] "Friday"
[1] "Saturday"

Example 3: For-Loop with List


# Create a list of numbers
my_list<- list(1, 2, 3, 4, 5)

# Loop through the list and print each element


for (iinseq_along(my_list)) {
current_element<- my_list[[i]]
print(paste("The current element is:", current_element))
}

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 2
Statistical Computing & R Programming

Output
[1] "The current element is: 1"
[1] "The current element is: 2"
[1] "The current element is: 3"
[1] "The current element is: 4"
[1] "The current element is: 5"

In this program, the seq_along() function is used to create a list of indices to loop through,
and double brackets [[]] are used to retrieve the current element during each loop iteration.

Example 4: For-Loop with Matrix


# Create a 3x3 matrix of integers
my_matrix<- matrix(1:9, nrow = 3)
# Loop through the matrix and print each element
for (iinseq_len(nrow(my_matrix))) {
for (j inseq_len(ncol(my_matrix))) {
current_element<- my_matrix[i, j]
print(paste("The current element is:", current_element))
}
}
Output
[1] "The current element is: 1"
[1] "The current element is: 4"
[1] "The current element is: 7"
[1] "The current element is: 2"
[1] "The current element is: 5"
[1] "The current element is: 8"
[1] "The current element is: 3"
[1] "The current element is: 6"
[1] "The current element is: 9"

The seq_len() function in R is used to generate a sequence of numbers from 1 to a specified


number. The program cycle through the matrix’s rows and columns using two for-loops,
each of which uses the [i, j] notation to retrieve the current member.

Example 4: For-Loop with DataFrame


# Create a dataframe with some sample data
my_dataframe<- data.frame(
Name = c("Joy", "Juliya", "Boby", "Marry"),
Age = c(40, 25, 19, 55),
Gender = c("M", "F", "M", "F")
)
# Loop through the dataframe and print each row
for (iinseq_len(nrow(my_dataframe))) {
current_row<- my_dataframe[i, ]
print(paste("The current row is:", toString(current_row)))
}

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 3
Statistical Computing & R Programming

Output
[1] "The current row is: Joy, 40, M"
[1] "The current row is: Juliya, 25, F"
[1] "The current row is: Boby, 19, M"
[1] "The current row is: Marry, 55, F"

2. WHILE
A while loop executes a statement repetitively until the condition is false.

Flow Diagram

Syntax:
while (expression)
{
statement
}

• Here, test_expression is evaluated and the body of the loop is entered if the result is
TRUE.
• The statements inside the loop are executed and the flow returns to evaluate the
test_expression again.
• This is repeated each time until test_expression evaluates to FALSE, in which case,
the loop exits.
Example
Program to print natural numbers from 1 to 5
i <- 1
while (i < 6) {
print(i)
i = i+1
}
Output
[1] 1
[1] 2
[1] 3
[1] 4

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 4
Statistical Computing & R Programming

[1] 5
Program to find the sum of first n natural numbers
sum = 0
# take input from the user
num = as.integer(readline(prompt = "Enter a number: "))
# use while loop to iterate until zero
while(num> 0)
{
sum = sum + num
num = num - 1
}
print(paste("The sum is", sum))
Output
Enter a number: 4
[1] "The sum is 10"

3. repeat loop
• A repeat loop is used to iterate over a block of code multiple number of times.
• There is no condition check in repeat loop to exit the loop.
• We must ourselves put a condition explicitly inside the body of the loop and use
the break statement to exit the loop. Failing to do so will result into an infinite loop.
Syntax
repeat {
statement
}
In the statement block, we must use the break statement to exit the loop.

Example
x <- 1
repeat {
print(x)
x = x+1
if (x == 6){
break
}

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 5
Statistical Computing & R Programming

}
Output
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5

break Statement
• A break statement is used inside a loop (repeat, for, while) to terminate the iterations
and the program continues with the next statement after the loop.
• In a nested looping situation, where there is a loop inside another loop, this
statement exits from the innermost loop that is being evaluated.

Syntax
if (test_expression) {
break
}

Example
x <- 1:5
for (val in x) {
if (val == 3){
break
}
print(val)
}

Output
[1] 1
[1] 2
• In this example, we iterate over the vector x, which has consecutive numbers
from 1 to 5.
• Inside the for loop we have used a if condition to break if the current value is equal
to 3.

next Statement
• A next statement is useful when we want to skip the current iteration of a loop without
terminating it.

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 6
Statistical Computing & R Programming

• On encountering next, the R skips further evaluation and starts the next iteration of
the loop.

Syntax
if (test_condition) {
next
}

• Here, test_condition is evaluated. If it results in a logical value of TRUE,


the next statement is executed.
• When the next statement is encountered, the current iteration of the loop is skipped,
and the program moves to the next iteration of the loop.

Example
x <- 1:5
for (val in x) {
if (val == 3){
next
}
print(val)
}
Output
[1] 1
[1] 2
[1] 4
[1] 5

Looping Over Non-vector Sets

• Looping statements like “for()” loop consume more memory and take more time in
iterating over large data set and executing a repetitive task.
• An alternative method to overcome these drawbacks of looping statements is set of
“Apply Family” functionsthat allows to repetitively perform a specified functions
like;sum(), mean() etc. across a vector, list, matrix, or data frame.

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 7
Statistical Computing & R Programming

• Following are the “Apply Family” functions:


1. apply()
2. lapply()
3. sapply()
4. tapply()

1. apply()
• This function applies a given function over the margins (row/column) of a given
array, matrices and data frames to find aggregate values likesum, mean, median,
standard deviation etc.
Syntax:
apply(array, margins, function)
array - list of elements
margins - dimension of the array along which the function needs to be applied
function - the operation which you want to perform

Example
# Creating a matrix
A = matrix(1:9, 3, 3)
print(A)
# Applying apply() over row of matrix
# Here margin 1 is for row
r = apply(A, 1, sum)
print(r)

# Applying apply() over column of matrix


# Here margin 2 is for column
c = apply(A, 2, sum)
print(c)

Output:
[, 1] [, 2] [, 3]
[1, ] 1 4 7
[2, ] 2 5 8
[3, ] 3 6 9

[1] 12 15 18
[1] 6 15 24

2. lapply()
• lapply( ) can be applied on dataframes,lists and vectors.
• It is applied on every element of the object.
• It returns a list as output.
• The length of input and output object are equal.
Syntax
lapply(X, FUN)
Example
> x <- matrix(1:4,2,2)
>x
[,1] [,2]

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 8
Statistical Computing & R Programming

[1,] 1 3
[2,] 2 4
>lapply(x,sqrt)
[[1]]
[1] 1
[[2]]
[1] 1.414214
[[3]]
[1] 1.732051
[[4]]
[1] 2
3. sapply()
• Like lapply() it is applied on a list, vector, or data frame but returns an array or
matrix object of the same length instead of list.

Syntax
sapply(X, FUN)
# create a list with 2 elements
x = (a=1:10,b=11:20) # mean of values using sapply
sapply(x, mean)
Output
ab
5.5 15.5

4. tapply( )
• The tapply() is used to compute statistical measures (mean, median, min, max, etc..)
or applyuser defined function on factor variable in a vector.
• It breaks the data set into groups and applies a function to each group.
• For example, in an organization, if we have data of employee salary and we want to
find the mean salary for male and female, then we can use tapply() function with
male and female as factor variable.

Syntax
tapply( x, index, fun )
where;
x - is the input vector or an object.
Index - the factor vector that is used to distinguish the data.
Fun - the function applied to the input data.

Example
# Find the age of youngest male and female
data <- data.frame(name=c("Amy","Max","Ray","Kim","Sam","Eve","Bob"),
age=c(24, 22, 21, 23, 20, 24, 21),
gender=factor(c("F","M","M","F","M","F","M")))
data
name age gender
1 Amy 24 F
2 Max 22 M
3 Ray 21 M
4 Kim 23 F

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 9
Statistical Computing & R Programming

5 Sam 20 M
6 Eve 24 F
7 Bob 21 M
tapply(data$age, data$gender, min)
F M
23 20

Reserved Words
• Reserved words are a set of words that have special meaning and cannot be used as
an identifier (variable name, function name etc.).
• This list is obtained by typing help(reserved) or ?reserved at the R command
prompt.
if else repeat while function

for in next break TRUE

FALSE NULL Inf NaN NA

NA_integer NA_real NA_complex_ NA_character …..

Conditional Statements
The conditional statement is decision making statement in a program. The following are the
conditional statement in R
1. if()
2. if-else
3. if-else-if statement
4. Nested if-else statement
5. ifelse() function

1. if()
• It is used to decide whether a certain statement or block of statements should be
executed or not.

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 10
Statistical Computing & R Programming

Syntax
if (expression) {
#statement to execute if condition is true
}
• If the expression is true, the statement gets executed.
• If the expression is FALSE, nothing happens.
• The expression can be a logical/numerical vector, but only the first element is taken
into consideration.
• In the case of numeric vector, zero is taken as FALSE, rest as TRUE.

Example
# R program to illustrate if statement
# assigning value to variable a
a <-5

# condition
if(a > 0)
{
print("Positive Number") # Statement
}
Output:
Positive Number

2. If-Else
Syntax
if (condition) {
# code to be executed if condition is TRUE
} else {
# code to be executed if condition is FALSE
}

Flow Diagram

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 11
Statistical Computing & R Programming

If the Boolean expression evaluates to true, then the if block of code is executed,
otherwise else block of code will be executed.

Example
x <- 5
# Check value is less than or greater than 10
if(x > 10)
{
print(paste(x, "is greater than 10"))
} else
{
print(paste(x, "is less than 10"))
}
Output
[1] "5 is less than 10"

3. if-else-if statement
• Used to test more than one conditions
Syntax
if(test_expression1) {
# code block 1
} else if (test_expression2){
# code block 2
} else {
# code block 3
}

Here,
If test_expression1 evaluates to True, the code block 1 is executed.
If test_expression1 evaluates to False, then test_expression2 is evaluated.
If test_expression2 is True, code block 2 is executed.
If test_expression2 is False, code block 3 is executed.

Example
x <- 0
# check if x is positive or negative or zero
if (x > 0) {
print("x is a positive number")
} else if (x < 0) {
print("x is a negative number")
} else {
print("x is zero")
}
Output
[1] "x is zero"

4. Nested if-else statement


• The if...else statements inside another if...else blocks is called nested if...else
statement.

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 12
Statistical Computing & R Programming

Example
x <- 41

if (x > 10) {
print("Above ten")
if (x > 20) {
print("and also above 20!")
} else {
print("but not above 20.")
}
} else {
print("below 10.")
}

Output
[1] "Above ten"
[1] "and also above 20!"

5. ifelse() function
• The ifelse() function is a conditional function that perform element-wise conditional
operations in vectors or data frames.

Syntax
ifelse(test_expression, x, y)

Here,
o text_expression – A logical condition or a logical vector that specifies the
condition to be evaluated. It can be a single logical value or a vector of
logical values.
o x - The value or expression to be returned when the condition is true. It can
be a single value, vector, or an expression.
o y - The value or expression to be returned when the condition is false. It can
be a single value, vector, or an expression.
• The return value is a vector with the same length as test_expression.

Example
# create a vector
a = c(5,7,2,9)
# check if each element in a is even or odd
ifelse(a %% 2 == 0,"even","odd")

Output
[1] "odd" "odd" "even" "odd"

Operators
• Operators are the symbols directing the compiler to perform various kinds of
operations between the operands.

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 13
Statistical Computing & R Programming

• Different operation include; mathematical, logical, and decision operations on a set


of Complex Numbers, Integers, and Numerical operands.

Types of operators
1. Arithmetic operators.
2. Relational operators.
3. Logical operators.
4. Assignment operators.
5. Miscellaneous Operators

Arithmetic operators:
Arithmetic operators are used to carry out mathematical operations. Following is the list of
arithmetic operators:

Operator Description Example Result


a <- c (1, 0.1)
+ Add two vectors b <- c (2.33, 4) [1]3.33 4.10
print (a+b)
a <- 6
Subtracts second vector from the [1]-2.4
− b <- 8.4
first
print (a-b)
B= c(4,4)
Multiplies corresponding elements of [1] 20 20
* C= c(5,5)
vectors
print (B*C)
a <- 10
Divide the first vector with the [1] 2
/ b <- 5
second
print (a/b)
list1<- c(2, 22)
Give the remainder of the first vector [1]0 2
%% list2<-c(2,4)
divide with the second
print(list1 %% list2)
The result of division of first vector v <- c( 2,5.5,6) [1] 0 1 1
%/% with second (quotient) t <- c(8, 3, 4)
print(v%/%t)
The first vector raised to the v <- c( 2,5.5,6) [1] 256.000
^ exponent of second vecto t <- c(8, 3, 4) print(v^t) 166.375
1296.000

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 14
Statistical Computing & R Programming

Relational operators
Relational operators are used to compare between values.Each element of the first vector is
compared with the corresponding element of the second vector. The result of comparison is a
Boolean value.
Operator Description Example Result
Checks if each element of the v <- c(2,5.5,6,9)
first vector is greater than the t <- c(8,2.5,14,9)
> [1] FALSE TRUE FALSE FALSE
corresponding element of the print(v>t)
second vector.
Checks if each element of the v <- c(2,5.5,6,9) t
first vector is less than the <- c(8,2.5,14,9)
< [1] TRUE FALSE TRUE FALSE
corresponding element of the print(v < t)
second vector.
Checks if each element of the v <- c(2,5.5,6,9)
first vector is equal to the t <- c(8,2.5,14,9)
== [1] FALSE FALSEFALSE TRUE
corresponding element of the print(v == t)
second vector.
Checks if each element of the v <- c(2,5.5,6,9)
first vector is less than or t <- c(8,2.5,14,9)
<= [1] TRUE FALSE TRUE TRUE
equal to the corresponding print(v<=t)
element of the second vector.
Checks if each element of the v <- c(2,5.5,6,9)
first vector is greater than or t <- c(8,2.5,14,9)
>= [1] FALSE TRUE FALSE TRUE
equal to the corresponding print(v>=t)
element of the second vector.
Checks if each element of the v <- c(2,5.5,6,9)
first vector is unequal to the t <- c(8,2.5,14,9)
!= [1] TRUE TRUETRUE FALSE
corresponding element of the print(v!=t)
second vector.

Logical Operators
It is applicable only to vectors of type logical, numeric or complex. Zero is considered
FALSE and non-zero numbers are taken as TRUE. Each element of the first vector is
compared with the corresponding element of the second vector. The result of comparison is a
Boolean value.

Operator Description Example Result


It is called Element-wise v <- c(3,1,TRUE,2+3i) [1] TRUE TRUE FALSE TRUE
Logical AND operator. It t <- c(4,1,FALSE,2+3i)
combines each element of the print(v&t)
first vector with the
&
corresponding element of the
second vector and gives a
output TRUE if both the
elements are TRUE.
It is called Element-wise v <- c(3,0,TRUE,2+2i) [1] TRUE FALSE TRUE TRUE
Logical OR operator. It t <- c(4,0,FALSE,2+3i)
combines each element of the print(v|t)
first vector with the
|
corresponding element of the
second vector and gives a
output TRUE if one the
elements is TRUE.

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 15
Statistical Computing & R Programming

It is called Logical NOT v <- c(3,0,TRUE,2+2i) [1] FALSE TRUE FALSE FALSE
operator. Takes each element of print(!v)
!
the vector and gives the
opposite logical value.

The logical operator && and || considers only the first element of the vectors and give a
vector of single element as output.

Operator Description Example Result


Called Logical AND operator. Takes v <- c(3,0,TRUE,2+2i)
first element of both the vectors and t <- c(1,3,TRUE,2+3i)
&& [1] TRUE
gives the TRUE only if both are print(v&&t)
TRUE.
Called Logical OR operator. Takes v <- c(0,0,TRUE,2+2i)
first element of both the vectors and t <- c(0,3,TRUE,2+3i)
|| [1] FALSE
gives the TRUE if one of them is print(v||t)
TRUE.

Assignment Operators

These operators are used to assign values to vectors.


Operator Description Example Result
Left Assignment v1 <- c(3,1,TRUE,2+3i) [1] 3+0i 1+0i 1+0i 2+3i
v2 <<- c(3,1,TRUE,2+3i) [1] 3+0i 1+0i 1+0i 2+3i
<-
v3 = c(3,1,TRUE,2+3i) [1] 3+0i 1+0i 1+0i 2+3i
=
print(v1)
<<-
print(v2)
print(v3)
Right Assignment c(3,1,TRUE,2+3i) -> v1 [1] 3+0i 1+0i 1+0i 2+3i
c(3,1,TRUE,2+3i) ->> v2 [1] 3+0i 1+0i 1+0i 2+3i
-> or ->>
print(v1)
print(v2)

Miscellaneous Operators
Operator Description Example Result
Colon operator. It creates the v <- 2:8 [1] 2 3 4 5 6 7 8
: series of numbers in print(v)
sequence for a vector.
This operator is used to v1 <- 8 [1] TRUE [1]
identify ifan element belongs v2 <- 12 FALSE
%in% to a vector. t <- 1:10
print(v1 %in% t)
print(v2 %in% t)
This operator is used to M = matrix( c(2,6,5,1,10,4), nrow [,1] [,2]
multiply a matrix with its = 2,ncol = 3,byrow = TRUE) [1,] 65 82
%*% t = M %*% t(M)
transpose. [2,] 82 117
print(t)

Functions:
• A function is a set of statements organized together to perform a specific task.
• In R, a function is an object which may or may not require the arguments passed to it
to accomplish the defined task of the function.

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 16
Statistical Computing & R Programming

• When the function is called the R interpreter pass the control to the function, along
with the arguments. The function performs its task then it returns control to the
interpreter along with the result which may be stored in other objects.
• Function can be classified as

1. Built-in/System defined (SDF)/Library functions.


o R has rich set of in-built functions which can be directly called in the program
without defining them. They are defined and stored in R packages.
o Example of built-in functions: seq(), mean(), max(), sum(x), paste() etc.

Example
# Create a sequence of numbers from 32 to 44.
print(seq(32,44))
# Find mean of numbers from 25 to 82.
print(mean(25:82))
# Find sum of numbers frm 41 to 68.
print(sum(41:68))

Output
[1] 32 33 34 35 36 37 38 39 40 41 42 43 44
[1] 53.5
[1] 1526

2. User defined functions (UDF).


These function do not exist, thus the programmer/user have to define them
before they can be used.

User Defined Functions (UDF)


Function Definition
• An R function is created by using the keyword function.

Syntax
function_name <- function(arg_1, arg_2, ...)
{
Function body
}
where;
• Function Name – The function is stored in R environment in this name as an object.
• Arguments − An argument is a placeholder. When a function is invoked, the value to
the argument is passed. Arguments are optional; that is, a function may contain no
arguments. Also arguments can have default values.
• Function Body − The function body contains a collection of statements that defines
what the function does.
• Return Value − The value returned wutn the function is called. It is the last
expression in the function body to be evaluated.

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 17
Statistical Computing & R Programming

Example 1: Function definition and Function call with argument


# Create a function to print squares of numbers in sequence.
new.function <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}
# Call the function new.function with argument.
#Argument passed to the function is 6
new.function(6)

Output
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36

Example 2: Function definition and Function call without argument

# Create a function without an argument.


new.function <- function() {
for(i in 1:5) {
print(i^2)
}
}

# Call the function without supplying an argument.


new.function()

Output
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25

Default Arguments
• The values to the default argument are assigned in the function definition.
• When the function is invoked without argument the default value are assigned to the
argument.
• If the values as passed at function call, the new values are assigned to the argument.

Example
# Create a function with default arguments.
new.function <- function(a = 3, b = 6) {
result <- a * b
print(result)

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 18
Statistical Computing & R Programming

# Call the function without giving any argument.


# Default values are used by the function
new.function()

Output
[1] 18

# Call the function with giving new values of the argument.


# Overriding the Default values with the values passed as function argument
new.function(9,5)
Output
[1] 45

Default Values for Arguments


• Default values for arguments in R functions are parameters that assume a default
value if no value is passed when the function is invoked.
• This feature provides flexibility, allowing the function to execute without requiring all
argument values explicitly passed.

Example
> my_matrix<- matrix (1:12, 4, 3, byrow= TRUE)

• In the above example byrow is the default argument in matrix() and FALSE is the
default value to this default argument.
• Implicitly (if not mentioned in function) byrow argument takes default value FALSE
and fills the elements in the matrix column wise.
• Since byrow is explicitly mentioned and is set to TRUE the elements in the matrix are
filled row wise.

Lazy Evaluation of Function


Lazy evaluation is an evaluation strategy which holds the evaluation of an expression
until its value is needed.
Example
fun <- function(a, b){
a * 10
}
fun(4)
[1] 40

Here,
b is defined as a function argument, but never evaluated. R never tries to look for the
value of b. So no error. This strategy is called “lazy” as it does “the strict minimum” of
evaluation

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 19
Statistical Computing & R Programming

Advantages
• It allows the language runtime to discard sub-expressions that are not directly linked
to the final result of the expression.
• It reduces the time complexity of an algorithm by discarding the temporary
computations and conditions.
• It allows the programmer to access components of data structures out-of-order after
initializing them, as long as they are free from any circular dependencies.
• It is best suited for loading data which will be infrequently accessed.
Lazy Evaluation − Drawbacks
• It forces the language runtime to hold the evaluation of sub-expressions until it is
required in the final result by creating thunks (delayed objects).
• Sometimes it increases space complexity of an algorithm.
• It is very difficult to find its performance because it contains thunks of expressions
before their execution.

Return Values
• A value that a function returns to the calling script or function when it completes its
task is called return value.
• The return value of a function can be any R object. Although the return value
is often a list, it could even be another function.
• You can transmit a value back to the caller by explicitly calling return().
Without this call, the value of the last executed statement will be returned by
default.
Syntax
• return(expression)
• Here, expression is evaluated, and its value is stored as the result of the function.
• The value specified in the return() statement is passed as the output of the function.

Function with explicit return() (Returns single value)


Example 1:
num <- function(x)
{
return(x*2)
}
> num(5)
[1] 10

Example 2:
#Code to check the given number and return positive, negative or zero.
check <- function(x) {
if (x > 0) {
result <- "Positive"
}
else if (x < 0) {
result <- "Negative"
}
else {
result <- "Zero"
}
return(result) }

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 20
Statistical Computing & R Programming

check(1)
check(-10)
check(0)

Output
[1] "Positive"
[1] "Negative"
[1] "Zero"

Function without explicit return()


Example 1:
num <- function(x)
{
x*2
}
> num(5)
[1] 10

Example 2:
check <- function(x) {
if (x > 0) {
result <- "Positive"
}
else if (x < 0) {
result <- "Negative"
}
else {
result <- "Zero"
}
result
}

check(1)
check(-10)
check(0)
Output
[1] "Positive"
[1] "Negative"
[1] "Zero"

Example: Functions with return() (Returns multiple values)


multi_return <- function() {
my_list <- list("color" = "red", "size" = 20, "shape" = "round")
return(my_list)
}
# call function multi_return() and assign the result to variable a
a <- multi_return()
a$color
a$size
a$shape

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 21
Statistical Computing & R Programming

Output
[1] "red"
[1] 20
[1]"round"

Deciding whether to Explicitly Call return()


• Explicit call of return () lengthens execution time. Unless the function is very short,
the time saved is negligible.
• If there is no explicit return value in a function, then it would simply return the last
evaluated expression.
• Example
h<-function(x,y)
{
a<-x+y
b<-x-y
c<-x*y
}

r<-h(2,3)
r
[1] 6
• In the above example, h() has three evaluated statements but only the last evaluated i.e
c<-2*3 is returned.
• In order to return other values, like; a and b the return() has to be called explicitly.
• Example
h<-function(x,y)
{
a<-x+y
b<-x-y
c<-x*y
return(a)
}

r1<-h(2,3)
r1
[1] 5
• Explicit call of return() function enhances the readability of the program.

Returning Complex Objects


To return complex objects (multiple values), the values are placed in a list and then the list is
returned.
Example
res2<-function()
{
v<-c(1,2,5,3,8)
m<-matrix(1:8,ncol=4)
v1<-mean(v)
m1<-min(m)
L<-list(vec=v1, mat=m1)
return(L) }

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 22
Statistical Computing & R Programming

res2()
$vec
[1] 3.8

$mat
[1] 1

Functions are Object


• R treats functions as objects. So, if we type the function name, the code inside the
function is displayed.
• Example
Consider the function res2()
res2<-function()
{
v<-c(1,2,5,3,8)
m<-matrix(1:8,ncol=4)
v1<-mean(v)
m1<-min(m)
L<-list(vec=v1,mat=m1)
return(L)
}
• If the function name is typed without the parenthesis the content of the function is
displayed instead of calling the function.
• Example
>res2
function()
{
v<-c(1,2,5,3,8)
m<-matrix(1:8,ncol=4)
v1<-mean(v)
m1<-min(m)
L<-list(vec=v1,mat=m1)
return(L)
}

No pointers in R
• R does not have variables corresponding to pointers or references like C language.
• This can make programming more difficult in some cases.
• The fundamental thought is to create a class constructor and have every instantiation
of the class be its own environment.
• One can then pass the object/condition into a function and it will be passed by
reference instead of by value, because unlike other R objects, environments are not
copied when passed to functions.
• Changes to the object in the function will change the object in the calling frame.
• In this way, one can operate on the object and change internal elements without
having to create a copy of the object when the function is called, nor pass the entire
object back from the function.
• For large objects, this saves memory and time.

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 23
Statistical Computing & R Programming

• For example, you cannot write a function that directly changes its arguments.
> x <- c(12,45,6)
> sort(x)
[1] 6 12 45
>x
[1] 12 45 6
• The argument to sort() does not change. If we do want x to change in this R code, the
solution is to reassign the arguments:
> x <- sort(x)
>x
[1] 6 12 45
• If a function has several output then a solution is to gather them together into a list,
call the function with this list as an argument, have the function return the list, and
then reassign to the original list.
• An example is the following function, which determines the indices of odd and even
numbers in a vector of integers:
> y <-function(v){
odds <- which(v %% 2 == 1)
evens <- which(v %% 2 == 0)
list(o=odds,e=evens)
}

> y(c(2,34,1,5))
$o
[1] 3 4
$e
[1] 1 2

Recursion
A function that calls itself is called a recursive function and this technique is known as
recursion. Recursive functions solve problems in layers.

Syntax
Recurse<-function()
{
-----
-----
Recurse()
}

Example
# Recursive function to find factorial
recursive.factorial <- function(x)
{
if (x == 0) return (1)
else return (x * recursive.factorial(x-1))
}

> recursive.factorial(5)
[1] 120

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 24
Statistical Computing & R Programming

A Quicksort Implementation

• Quick sort is also known as Partition-Exchange sort and is based on Divide and
conquer Algorithm design method.
• This was proposed by C.A.R Hoare.
• The basic idea of quick sort is very simple.
• We consider one element at a time (pivot element). We have to move the pivot
element to the final position that it should occupy in the final sorted list. While
identifying this position, we arrange the elements, such that the elements to the left of
the pivot element will be less than pivot element & elements to the right of the pivot
element will be greater than pivot element. There by dividing the list by 2 parts.
• We have to apply quick sort on these 2 parts recursively until the entire list is sorted.
• For instance, suppose we wish to sort the vector (5,4,12,13,3,8,88). We first compare
everything to the first element, 5, to form two subvectors: one consisting of the
elements less than 5 and the other consisting of the elements greater than or equal to
5. That gives us subvectors (4,3) and (12,13,8,88).
• We then call the function on the subvectors, returning (3,4) and (8,12,13,88). We
string those together with the 5, yielding (3,4,5,8,12,13,88), as desired. R‘s vector-
filtering capability and its c() function make implementation of Quicksort quite easy.

# Quicksort recursive function


qs <- function(x) {
if (length(x) <= 1) return(x)
pivot <- x[1]
therest <- x[-1]
sv1 <- therest[therest < pivot]
sv2 <- therest[therest >= pivot]
sv1 <- qs(sv1)
sv2 <- qs(sv2)
return(c(sv1,pivot,sv2)) }

> qs(c(12,6,7,34,3))
[1] 3 6 7 12 34

Binary search tree


• The nature of binary search trees implies that at any node, all of the elements in the
node‘s left subtree are less than or equal to the value stored in this node, while the
right subtree stores the elements that are larger than the value in this mode.

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 25
Statistical Computing & R Programming

• In the example tree, where the root node contains 8, all of the values in the left
subtree-5, 2 and 6-are less than 8, while 20 is greater than 8.

Program
#Note that it includes only routines to insert new items and to traverse the tree.
# storage is in a matrix, say m, one row per node of the tree; a link i in the tree means the vector
#m[i,] = (u,v,w); u and v are the left and right links, and w is the stored value; null links have the
#value
#NA; the matrix is referred to as the list (m,nxt,inc), where m is the matrix, nxt is the next empty row
#to be used, and inc is the number of rows of expansion to be allocated when the matrix becomes full
# initializes a storage matrix, with initial stored value firstval

newtree <- function(firstval,inc) {


m <- matrix(rep(NA,inc*3),nrow=inc,ncol=3)
m[1,3] <- firstval
return(list(mat=m,nxt=2,inc=inc))
}

# inserts newval into nonempty tree whose head is index hdidx in the storage space treeloc;
#note that return value must be reassigned to tree; inc is as in newtree() above

ins <- function(hdidx,tree,newval,inc) {


tr <- tree

# check for room to add a new element


tr$nxt <- tr$nxt + 1
if (tr$nxt > nrow(tr$mat))
tr$mat <- rbind(tr$mat,matrix(rep(NA,inc*3),nrow=inc,ncol=3))
newidx <- tr$nxt # where we'll put the new tree node
tr$mat[newidx,3] <- newval
idx <- hdidx # marks our current place in the tree
node <- tr$mat[idx,]
nodeval <- node[3]
while (TRUE) {
# which direction to descend, left or right?
if (newval <= nodeval) dir <- 1 else dir <- 2
# descend
# null link?
if (is.na(node[dir])) {
tr$mat[idx,dir] <- newidx
break
} else {
idx <- node[dir]
node <- tr$mat[idx,]
nodeval <- node[3]
}

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 26
Statistical Computing & R Programming

}
return(tr)
}

# print sorted tree via inorder traversal


printtree <- function(hdidx,tree) {
left <- tree$mat[hdidx,1]
if (!is.na(left)) printtree(left,tree)
print(tree$mat[hdidx,3])
right <- tree$mat[hdidx,2]
if (!is.na(right)) printtree(right,tree)
}

Error Handling
• Error Handling is a process of dealing with unwanted or anomalous errors which may
cause abnormal termination of the program during its execution.
• R provides two implementing mechanism in error handling:
1. Calling stop() or warning() functions
2. Using error options like “warn” or “warning.expression”.

Error handling functions


stop()
• It halts the evaluation of the current statement and generates a message argument.
• The control is returned to the top level.
• Example
#R Program to illustrate if-else function on vectors of variable length.
{
print("Enter Values :")
vnums<- scan();
result <- ifelse(vnums> 0, "Positive", ifelse(vnums<0,"Negetive", "Zero"))
result
}

Output
[1] "Enter Values :"
1: 4
2: 5
3: -7
4:
Read 3 items
[1] "Positive" "Positive" "Non-Positive"
waiting()
• Its evaluation depends on the value of the error option warn.
• If the value of the warning is negative then it is ignored. In case the value is 0 (zero)
they are stored and printed only after the top-level function completes its execution.
• If the value is 1 (one) then it is printed as soon as it has been encountered while if the
value is 2 (two) then immediately the generated warning is converted into an error.

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 27
Statistical Computing & R Programming

tryCatch()
• It helps to evaluate the code and assign the exceptions.
• An efficient and interactive way to debug the error.
• Three methods to handle conditions and errors are:
• try(): it helps to continue with the execution of the program even when an error
occurs.
• tryCatch(): it helps to handle the conditions and control what happens based on
the conditions.
• withCallingHandlers(): it is an alternative to tryCatch() that takes care of the local
handlers.
• Unlike other programming languages such as Java, C++ the try-catch-finally
statements are used as a function in R.
• The two conditions handled in tryCatch() are “errors” and “warnings”.

try-catch-finally in R
Syntax:
check = tryCatch({
expression
}, warning = function(w){
code that handles the warnings
}, error = function(e){
code that handles the errors
}, finally = function(f){
clean-up code
})
Example
# R program illustrating error handling
# Applying tryCatch
tryCatch(

# Specifying expression
expr = {
1+1
print("Everything was fine.")
},
# Specifying error message
error = function(e){
print("There was an error message.")
},
warning = function(w){
print("There was a warning message.")
},

finally = {
print("finally Executed")
}
)
Output:
[1] "Everything was fine."
[1] "finally Executed"

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 28
Statistical Computing & R Programming

withCallingHandlers()

• withCallingHandlers() is a variant of tryCatch().


• tryCatch() deals with exiting handlers while withCallingHandlers() deals with local
handlers.

Example:
# R program illustrating error handling
# Evaluation of tryCatch
check <- function(expression){
withCallingHandlers(expression,
warning = function(w){
message("warning:\n", w)
},
error = function(e){
message("error:\n", e)
},
finally = {
message("Completed")
})
}

check({10/2})
check({10/0})
check({10/'noe'})

Output

Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 29

You might also like