0% found this document useful (0 votes)
11 views11 pages

R Unit - 3

Uploaded by

Ranu Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

R Unit - 3

Uploaded by

Ranu Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT - 3

Conditions and loops, functions in R, Objects and classes, Recursion, Debugging


LOOPS - In R programming, we require a control structure to run a block of
code multiple times. Loops come in the class of the most fundamental and
strong programming concepts. A loop is a control statement that allows multiple
executions of a statement or a set of statements. The word ‘looping’ means
cycling or iterating. A loop asks a query, in the loop structure. If the answer to
that query requires an action, it will be executed. The same query is asked again
and again until further action is taken. Any time the query is asked in the loop, it
is known as an iteration of the loop. There are two components of a loop, the
control statement, and the loop body.

The control statement controls the execution of statements depending on the


condition and the loop body consists of the set of statements to be executed. In
order to execute identical lines of code numerous times in a program, a
programmer can simply use a loop.

There are three types of loops in R programming:


For Loop in R - This is a type of control statement that enables one to easily
construct a loop that has to run statements or a set of statements multiple times.
For 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
R – For loop Syntax:
for (value in sequence)
{
statement
}
FLOW DIAGRAM OF FOR LOOP
While Loop in R - It is a type of control statement which will run a statement
or a set of statements repeatedly unless the given condition becomes false. It is
also 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.
R – While loop Syntax:
while ( condition )
{
statement
}
While loop Flow Diagram:
Repeat Loop in R - It is a simple loop that will run the same statement or a
group of statements repeatedly until the stop condition has been encountered.
Repeat loop does not have any condition to terminate the loop, a programmer
must specifically place a condition within the loop’s body and use the
declaration of a break statement to terminate this loop. If no condition is present
in the body of the repeat loop then it will iterate infinitely.
R – Repeat loop Syntax:
repeat
{
statement

if( condition )
{
break
}
}
Repeat loop Flow Diagram:

To terminate the repeat loop, we use a jump statement that is the break keyword.
Below are some programs to illustrate the use of repeat loops in R
programming.
Jump Statements in Loop - We use a jump statement in loops to terminate the
loop at a particular iteration or to skip a particular iteration in the loop. The two
most commonly used jump statements in loops are:
● Break Statement: The break keyword is a jump statement that is used to
terminate the loop at a particular iteration.
Conditions - In R programming like that with other languages, there are
several cases where you might wish to conditionally execute any code. Here 'if'
and 'switch' functions of the R language can be implemented if you already
programmed condition-based code in other languages, Vectorized conditional
implementation via the if..else() function is also a characteristic of R.
Type of flow control statement in r
R programming provides three different types of statements that allows
programmers to control their statements within source code. These are
The 'if' Statement - The simplest form of decision controlling statement for
conditional execution is the 'if' statement. The 'if' produces a logical value (more
exactly, a logical vector having length one) and carries out the next statement
only when that value becomes TRUE. In other words, an 'if' statement is having
a Boolean expression followed by single or multiple statements.
IF …. Else statement - In this type of statement the 'if' statement is usually
followed by an optional 'else' statement that gets executed when the Boolean
expression becomes false. This statement is used when you will be having
multiple statements with multiple conditions to be executed.
Switch statement - A switch statement permits a variable to be tested in favor
of equality against a list of case values. In the switch statement, for each case
the variable which is being switched is checked. This statement is generally
used for multiple selection of condition based statement.
The basic syntax for programming switch-based conditional statements in R is

Syntax:
switch (test_expression, case1, case2, case3 .... caseN)

FUNCTIONS - A function is a set of statements organized together to perform


a specific task. R has a large number of in-built functions and the user can
create their own functions. In R, a function is an object so the R interpreter is
able to pass control to the function, along with arguments that may be necessary
for the function to accomplish the actions.

The function in turn performs its task and returns control to the interpreter as
well as any result which may be stored in other objects.

Function Definition

An R function is created by using the keyword function. The basic syntax of an


R function definition is as follows −

function_name <- function(arg_1, arg_2, ...) {


Function body
}

Function Components

The different parts of a function are −

● Function Name − This is the actual name of the function. It is


stored in the R environment as an object with this name.
● Arguments − An argument is a placeholder. When a function is
invoked, you pass a value to the argument. 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 return value of a function is the last
expression in the function body to be evaluated.
R has many in-built functions which can be directly called in the program
without defining them first. We can also create and use our own functions
referred as user-defined functions.

Built-in Function - Simple examples of in-built functions are seq(), mean(),


max(), sum(x), and paste(...), etc. They are directly called by user-written
programs.

User-defined Function - We can create user-defined functions in R. They are


specific to what a user wants and once created they can be used like the built-in
functions.

Calling a Function with Argument Values (by position and by name) - The
arguments to a function call can be supplied in the same sequence as defined in
the function or they can be supplied in a different sequence but assigned to the
names of the arguments.)

Calling a Function with Default Argument - We can define the value of the
arguments in the function definition and call the function without supplying any
argument to get the default result. But we can also call such functions by
supplying new values of the argument and get a non-default result.
OBJECTS
Every programming language has its own data types to store values or any
information so that the user can assign these data types to the variables and
perform operations respectively. Operations are performed accordingly to the
data types.

These data types can be character, integer, float, long, etc. Based on the data
type, memory/storage is allocated to the variable. For example, in C language
character variables are assigned 1 byte of memory, integer variables with 2 or 4
bytes of memory, and other data types have different memory allocations for
them. Unlike other programming languages, variables are assigned to objects
rather than data types in R programming.

Type of Objects
There are 5 basic types of objects in the R language:

Vectors - Atomic vectors are one of the basic types of objects in R


programming. Atomic vectors can store homogeneous data types such as
characters, doubles, integers, raw, logical, and complex. A single-element
variable is also said to be a vector.
Lists - A list is another type of object in R programming. A list can contain
heterogeneous data types such as vectors or another list.
Matrices - To store values as a 2-Dimensional array, matrices are used in R.
Data, a number of rows, and columns are defined in the matrix() function.
Syntax: matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames
= NULL)

Factors - Factor object encodes a vector of unique elements (levels) from the
given data vector.
Arrays - array() function is used to create an n-dimensional array. This function
takes the dim attribute as an argument and creates the required length of each
dimension as specified in the attribute.
Syntax: array(data, dim = length(data), dimnames = NULL)

Data Frames - Data frames are 2-dimensional tabular data objects in R


programming. Data frames consist of multiple columns and each column
represents a vector. Columns in data frames can have different modes of data,
unlike matrices.
CLASSES
Classes and Objects are basic concepts of Object-Oriented Programming that
revolve around real-life entities. Everything in R is an object. An object is
simply a data structure that has some methods and attributes. A class is just a
blueprint or a sketch of these objects. It represents the set of properties or
methods that are common to all objects of one type.

Unlike most other programming languages, R has a three-class system.


These are S3, S4, and Reference Classes.

S3 Class - S3 is the simplest yet the most popular OOP system and it lacks
formal definition and structure. An object of this type can be created by just
adding an attribute to it. Following is an example to make things more clear:
In S3 systems, methods don’t belong to the class. They belong to generic
functions. It means that we can’t create our own methods here, as we do in other
programming languages like C++ or Java. But we can define what a generic
method (for example print) does when applied to our objects.

S4 Class - Programmers of other languages like C++, and Java might find S3 to
be very much different than their normal idea of classes as it lacks the structure
that classes are supposed to provide. S4 is a slight improvement over S3 as its
objects have a proper definition and it gives a proper structure to its objects.
As shown in the above example, setClass() is used to define a class and new() is
used to create the objects.

The concept of methods in S4 is similar to S3, i.e., they belong to generic


functions. The following example shows how to create a method:

Reference Class - Reference Class is an improvement over S4 Class. Here the


methods belong to the classes. These are much similar to object-oriented classes
of other languages. Defining a Reference class is similar to defining S4 classes.
We use setRefClass() instead of setClass() and “fields” instead of “slots”.

RECURSION
Recursion, in the simplest terms, is a type of looping technique. It exploits the
basic working of functions in R. Recursion is when the function calls itself. This
forms a loop, where every time the function is called, it calls itself again and
again and this technique is known as recursion. Since the loops increase the
memory we use the recursion. The recursive function uses the concept of
recursion to perform iterative tasks they call themselves, again and again, which
acts as a loop. These kinds of functions need a stopping condition so that they
can stop looping continuously.

Recursive functions call themselves. They break down the problem into smaller
components. The function() calls itself within the original function() on each of
the smaller components. After this, the results will be put together to solve the
original problem.

Key Features of R Recursion


● The use of recursion, often, makes the code shorter and it also looks
clean.
● It is a simple solution for a few cases.
● It expresses in a function that calls itself.

Applications of Recursion in R
● Recursive functions are used in many efficient programming
techniques like dynamic programming language(DSL) or divide and
conquer algorithms.
● In dynamic programming, for both top-down as well as bottom-up
approaches, recursion is vital for performance.
● In divide and conquer algorithms, we divide a problem into smaller
sub-problems that are easier to solve. The output is then built back up
to the top. Recursion has a similar process, which is why it is used to
implement such algorithms.
● In its essence, recursion is the process of breaking down a problem
into many smaller problems, these smaller problems are further broken
down until the problem left is trivial. The solution is then built back up
piece by piece.

DEBUGGING - Debugging is a process of cleaning a program code from bugs


to run it successfully. While writing codes, some mistakes or problems
automatically appear after the compilation of code and are harder to diagnose.
So, fixing it takes a lot of time and after multiple levels of calls.

Debugging in R is through warnings, messages, and errors. Debugging in R


means debugging functions. Various debugging functions are:

● Editor breakpoint
● traceback()
● browser()
● recover()

Editor Breakpoints -Editor Breakpoints can be added in RStudio by clicking to


the left of the line in RStudio or pressing Shift+F9 with the cursor on your line.
A breakpoint is the same as a browser() but it doesn’t involve changing codes.
Breakpoints are denoted by a red circle on the left side, indicating that debug
mode will be entered at this line after the source is run.
traceback() Function - The traceback() function is used to give all the
information on how your function arrived at an error. It will display all the
functions called before the error arrived called the “call stack” in many
languages, and R favors calling traceback.
traceback() function displays the error during evaluations. The call stack is read
from the function that was run(at the bottom) to the function that was run (at the
top). Also, we can use traceback() as an error handler which will display the
error immediately without calling traceback.

browser() Function - browser() function is inserted into functions to open R


interactive debugger. It will stop the execution of the function() and you can
examine the function with the environment of itself. In debug mode, we can
modify objects, look at the objects in the current environment, and also continue
executing.

browser[1]> command in consoles confirms that you are in debug mode. Some
commands to follow:

● ls(): Objects available in the current environment.


● print(): To evaluate objects.
● n: To examine the next statement.
● s: To examine the next statement by stepping into function calls.
● where: To print a stack trace.
● c: To leave the debugger and continue with execution.
● C: To exit the debugger and go back to the R prompt.

Also, debug() statement automatically inserts the browser() statement at the


beginning of the function.

recover() Function - recover() statement is used as an error handler and not like
the direct statement. In recover(), R prints the whole call stack and lets you
select which function browser you would like to enter. Then debugging session
starts at the selected location.

You might also like