R unit 2
R unit 2
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
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.
Output:
[1] "Sunday"
[1] "Monday"
[1] "Tuesday"
[1] "Wednesday"
[1] "Thursday"
[1] "Friday"
[1] "Saturday"
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.
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
}
Example
x <- 1:5
for (val in x) {
if (val == 3){
next
}
print(val)
}
Output
[1] 1
[1] 2
[1] 4
[1] 5
• 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
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)
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
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"
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
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:
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.
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.
Assignment Operators
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
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
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
Output
[1] 1
[1] 4
[1] 9
[1] 16
[1] 25
[1] 36
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
Output
[1] 18
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.
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.
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"
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"
Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 21
Statistical Computing & R Programming
Output
[1] "red"
[1] 20
[1]"round"
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.
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
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.
> qs(c(12,6,7,34,3))
[1] 3 6 7 12 34
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
# 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
Dr. Jalaja Udoshi, Prof. Sheela Mense, Dept. of BCA, GSS College, Bgm 26
Statistical Computing & R Programming
}
return(tr)
}
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”.
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()
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