R Unit - 3
R Unit - 3
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)
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
Function Components
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:
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)
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.
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.
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.
● Editor breakpoint
● traceback()
● browser()
● recover()
browser[1]> command in consoles confirms that you are in debug mode. Some
commands to follow:
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.