You are on page 1of 66

lOMoARcPSD|20574153

DS4015 BDA UNIT V KVL Notes

Master of Computer Applications (Anna University)

Studocu is not sponsored or endorsed by any college or university


Downloaded by BARATH S (htarab86@gmail.com)
lOMoARcPSD|20574153

DS4015 UNIT - V
UNIT - V R LANGUAGE

Overview, Programming structures: Control statements -Operators -Functions -


Environment and scope issues -Recursion -Replacement functions, R data
structures: Vectors -Matrices and arrays - Lists -Data frames -Classes,
Input/output, String manipulations.

( I ) OVERVIEW OF R LANGUAGE

Introduction to R Language
History of R Language
Features of R Language
Use of R Language
Applications of R Language

Introduction to R language

o R is a programming language and software environment for statistical


analysis, graphics representation and reporting.
o The core of R is an interpreted computer language which allows branching
and looping as well as modular programming using functions.
o R allows integration with the procedures written in the C, C++, .Net, Python
or FORTRAN languages for efficiency.
o R is one of the most important tool which is used by researchers, data
analyst, statisticians, and marketers for retrieving, cleaning, analyzing,
visualizing, and presenting data.

History of R Language

o R was created by Ross Ihaka and Robert Gentleman at the University of


Auckland, New Zealand, and is currently developed by the R Development
Core Team.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 1

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
o R is freely available under the GNU General Public License, and pre-
compiled binary versions are provided for various operating systems like
Linux, Windows and Mac.
o This programming language was named R, based on the first letter of first
name of the two R authors (Robert Gentleman and Ross Ihaka), and partly a
play on the name of the Bell Labs Language S.

Features of R Language

o It is a simple and effective programming language which has been well


developed.
o It is data analysis software.
o It is a well-designed, easy, and effective language which has the concepts of
user-defined, looping, conditional, and various I/O facilities.
o It has a consistent and incorporated set of tools which are used for data
analysis.
o For different types of calculation on arrays, lists and vectors, R contains a
suite of operators.
o It provides effective data handling and storage facility.
o It is an open-source, powerful, and highly extensible software.
o It provides highly extensible graphical techniques.
o It allows us to perform multiple calculations using vectors.
o R is an interpreted language.

Use of R Language

o It is a great resource for data analysis, data visualization, data science and
machine learning
o It provides many statistical techniques (such as statistical tests,
classification, clustering and data reduction)
o It is easy to draw graphs in R, like pie charts, histograms, box plot, scatter
plot, etc++
o It works on different platforms (Windows, Mac, Linux)
o It is open-source and free
o It has a large community support
o It has many packages (libraries of functions) that can be used to solve
different problems

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 2

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Applications of R Language

Facebook Google Twitter HRDAG FDA ANZ


Sunlight Foundation RealClimate NDAA XBOX ONE

( II ) PROGRAMMING STRUCTURES

Command Prompt
R Script
Comments
Keywords
Data Types
Variables

Command Prompt

o Installed the R environment set up in our system to work on the R command


prompt.
o After the installation of R environment setup, we can easily start R
command prompt by typing R in our Windows command prompt.
o When we press enter after typing R, it will launch interpreter, and we will
get a prompt on which we can code our program.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 3

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

R Script File

o The R script file is another way on which we can write our programs, and
then we execute those scripts at our command prompt with the help of R
interpreter known as Rscript.
o We make a text file and write the following code. We will save this file with
.R extension as:

Demo.R

string <-"Hello World!"


print(string)

To execute this file in Windows and other operating systems, the process will
remain the same as mentioned below.

When we press enter it will give us the following output:

Comments

o Comments can be used to explain R code, and to make it more readable.


o It can also be used to prevent execution when testing alternative code.
o Comments starts with a #.
o When executing code, R will ignore anything that starts with #.

Comments are generally used for the following purposes:

o Code Readability
o Explanation of the code or Metadata of the project
o Prevent execution of code
o To include resources

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 4

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Types of Comments

o Single-line Comments- Comment that only needs one line


o Multi-line Comments- Comment that requires more than one line.
o Documentation Comments- Comments that are drafted usually for a quick
documentation look-up

Single line Comments


# This is a comment
"Hello World!"

uses a comment at the end of a line of code:

"Hello World!" # This is a comment

R doesn’t support Multi-line and Documentation comments. It only supports


single-line comments drafted by a ‘#’ symbol.

Keywords

o A keyword is a word which is reserved by a program because it has a special


meaning.
o A keyword can be a command or a parameter.
o A keyword can't be used as a variable name. Keywords are also called as
"reserved names."

Data Types

o Variables can store data of different types, and different types can do
different things.
o In R, variables do not need to be declared with any particular type, and can
even change type after they have been set.
Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 5

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Example

my_var <- 30 # my_var is type of numeric


my_var <- "Sally" # my_var is now of type character (aka string)

[1] "Sally"

Basic Data Types

Basic data types in R can be divided into the following types:

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 6

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 7

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Variables in R

Variables are containers for storing data values.


R does not have a command for declaring a variable.
A variable is created the moment, first assign a value to it.
To assign a value to a variable, use the <- sign.
To output (or print) the variable value, just type the variable name:

Example

name <- "abc"


age <- 40

name # output "abc"


age # output 40

To use = as an assignment operator.


In R, we can use both = and <- as assignment operators.

Output/ Print Variable

To use a function to print/output variables in R. Just type the name of the variable.

name <- "John Doe"

name # auto-print the value of the name variable

Use a print() function to output variables.

name <- "John Doe"

print(name) # print the value of the name variable

Concatenate Elements

concatenate, or join, two or more elements, by using the paste() function.


To combine both text and a variable, R uses comma (,).

text1 <- "R is"


text2 <- "awesome"

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 8

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

paste(text1, text2)

[1] "R is awesome"

Variable Names

A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume).
variable names are case-sensitive!

Rules for R variables are:

o A variable name must start with a letter and can be a combination of letters,
digits, period(.) and underscore(_). If it starts with period(.), it cannot be
followed by a digit.
o A variable name cannot start with a number or underscore (_)
o Variable names are case-sensitive (age, Age and AGE are three different
variables)
o Reserved words cannot be used as variables (TRUE, FALSE, NULL, if...)

Example

# Legal variable names:

myvar <- "John"


my_var <- "John"
myVar <- "John"
MYVAR <- "John"
myvar2 <- "John"
.myvar <- "John"

# Illegal variable names:

2myvar <- "John"


my-var <- "John"
my var <- "John"
_my_var <- "John"
my_v@ar <- "John"
TRUE <- "John"

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 9

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
( III ) OPERATORS

An operator is a symbol which represents an action. An operator is a symbol which


tells the compiler to perform specific logical or mathematical manipulations. R
programming is very rich in built-in operators.

Types of Operators

1) Arithmetic Operators
2) Relational Operators
3) Logical Operators
4) Assignment Operators
5) Miscellaneous Operators

(1) Arithmetic Operators

o Arithmetic operators are the symbols which are used to represent


arithmetic math operations.
o The operators act on each and every element of the vector. There are
various arithmetic operators which are supported by R.

Examples for Arithmetic Operators

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 10

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

(2) Relational Operators

o A relational operator is a symbol which defines some kind of relation


between two entities.
o These include numerical equalities and inequalities.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 11

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
o A relational operator compares each element of the first vector with
the corresponding element of the second vector.
o The result of the comparison will be a Boolean value.

There are the following relational operators which are supported by R:

Examples for Relational Operators

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 12

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 13

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

(3) Logical Operators

o The logical operators allow a program to make a decision on the basis of


multiple conditions.
o In the program, each operand is considered as a condition which can be
evaluated to a false or true value.
o The value of the conditions is used to determine the overall value of the
op1 operator op2.
o Logical operators are applicable to those vectors whose type is logical,
numeric, or complex.
o The logical operator compares each element of the first vector with the
corresponding element of the second vector.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 14

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 15

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

4) Assignment Operator

An assignment operator is used to assign a new value to a variable. In R, these


operators are used to assign values to vectors. There are the following types of
assignment.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 16

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

5) Miscellaneous Operators

Miscellaneous operators are used for a special and specific purpose. These
operators are not used for general mathematical or logical computation. There are
the following miscellaneous operators which are supported in R.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 17

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

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.

Control Statements is of two types


Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 18

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Conditional Control Statement


Unconditional Control Statement

Conditional Control Statement

Conditional statements help you to make a decision based on certain conditions.


These conditions are specified by a set of conditional statements having boolean
expressions which are evaluated to a boolean value of true or false.

There are the following types of conditional statements in C.

Decision Making Statement / Selection Statement.


Iteration Statement / Looping Statement.

Unconditional Control Statement

Unconditional [control] statements are statements that execute without specific


conditions.

next & break.

Decision Making Statement

Decision making structures require the programmer to specify one or more


conditions to be evaluated or tested by the program, along with a statement or
statements to be executed if the condition is determined to be true, and optionally,
other statements to be executed if the condition is determined to be false.

If statement
If-Else statement
Nested If-else statement
If-Else If ladder
Switch statement

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 19

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

if Statement

An if statement consists of a Boolean expression followed by one or more


statements.

Flow Diagram

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 20

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Syntax

If the Boolean expression evaluates to be true, then the block of code inside the if
statement will be executed. If Boolean expression evaluates to be false, then the
first set of code after the end of the if statement (after the closing curly brace) will
be executed.

Example

Output

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 21

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

if-else Statement

An if statement can be followed by an optional else statement which executes


when the boolean expression is false.

Syntax

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

Flow Diagram

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 22

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
Example

Output

if-else if ladder / else if Statement

This statement is also known as nested if-else statement. The if statement is


followed by an optional else if..... else statement. This statement is used to test
various condition in a single if......else if statement.

There are some key points which are necessary to keep in mind when we are using
the if.....else if.....else statement. These points are as follows:

1. if statement can have either zero or one else statement and it must
come after any else if's statement.
2. if statement can have many else if's statement and they come before
the else statement.
3. Once an else if statement succeeds, none of the remaining else
if's or else's will be tested.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 23

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Flow Diagram

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 24

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Example

Output

Nested if Statement

When we have an if-else block as an statement within an if block or


optionally within an else block, then it is called as nested if else statement.
When an if condition is true then following child if condition is validated
and if the condition is wrong else statement is executed, this happens within
parent if condition.
If parent if condition is false then else block is executed with also may
contain child if else statement.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 25

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Syntax

Flow Chart

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 26

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Example

Output

Switch Statement

o A switch statement is a selection control mechanism that allows the value of


an expression to change the control flow of program execution via map and
search.
o The switch statement is used in place of long if statements which compare a
variable with several integral values.
o It is a multi-way branch statement which provides an easy way to dispatch
execution for different parts of code.
o This code is based on the value of the expression.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 27

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Key points to remember in Switch Statement

o If expression type is a character string, the string is matched to the listed


cases.
o If there is more than one match, the first match element is used.
o No default case is available.
o If no case is matched, an unnamed case is used.

Syntax

Flow Diagram

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 28

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

1) Based on Index

If the cases are values like a character vector, and the expression is evaluated to a
number than the expression's result is used as an index to select the case.

Example

Output

2) Based on Matching Value

When the cases have both case value and output value like ["case_1"="value1"],
then the expression value is matched against case values. If there is a match with
the case, the corresponding value is the output.

Example

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 29

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Output

Iteration Statement / Looping Statement

To execute a block of code several number of times. In general, statements are


executed sequentially. The first statement in a function is executed first, followed
by the second, and so on.
Programming languages provide various control structures that allow for more
complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple
times

repeat statement.
while statement.
for loop statement.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 30

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

for loop

To iterate over the elements of a list, data frame, vector, matrix, or any
other object. It means the for loop can be used to execute a group of statements
repeatedly depending upon the number of elements in the object. 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.

In R, a for loop is defined as :

o It starts with the keyword for like C or C++.


o Instead of initializing and declaring a loop counter variable, we declare a
variable which is of the same type as the base type of the vector, matrix, etc.,
followed by a colon, which is then followed by the array or matrix name.
o In the loop body, use the loop variable rather than using the indexed array
element.

Syntax

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 31

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
Flow Chart

Example

Output

Repeat loop

The Repeat loop executes the same code again and again until a
stop condition is met.

o A repeat loop is used to iterate a block of code.


o It is a special type of loop in which there is no condition to exit from the
loop.
o For exiting, we include a break statement with a user-defined condition.
Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 32

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
o This property of the loop makes it different from the other loops.
o A repeat loop constructs with the help of the repeat keyword in R.
o It is very easy to construct an infinite loop in R.

Syntax

Flow Chart

Example

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 33

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Output

While Loop

The While loop executes the same code again and again until a stop
condition is met.

o When the exact number of iterations of a loop is not known beforehand.


o It executes the same code again and again until a stop condition is met.
o While loop checks for the condition to be true or false n+1 times rather
than n times.
o This is because the while loop checks for the condition before entering the
body of the loop.

Syntax

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 34

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Flow Chart

Example

Output

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 35

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Unconditional Control Statement / Loop Control Statement

Loop control statements change execution from its normal sequence. When
execution leaves a scope, all automatic objects that were created in that scope are
destroyed.

Break Statement

In the R language, the break statement is used to break the execution and for an
immediate exit from the loop. In nested loops, break exits from the innermost loop
only and control transfer to the outer loop.
It is useful to manage and control the program execution flow. We can use it to
various loops like: for, repeat, etc.

There are basically two usages of break statement which are as follows:
o When the break statement is inside the loop, the loop terminates
immediately and program control resumes on the next statement after the
loop.
o It is also used to terminate a case in the switch statement.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 36

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
Syntax

break;

Flow Chart

Example

Output

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 37

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
Next statement

The next statement in R programming language is useful when we want to skip the
current iteration of a loop without terminating it. On encountering next, the R
parser skips further evaluation and starts next iteration of the loop.

Syntax

next;

Flow Chart

Example

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 38

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Output

R ENVIRONMENT & SCOPE ISSUES

R is an integrated suite of software facilities for data manipulation, calculation and


graphical display. It includes

o an effective data handling and storage facility,


o a suite of operators for calculations on arrays, in particular matrices,
o a large, coherent, integrated collection of intermediate tools for data
analysis,
o graphical facilities for data analysis and display either on-screen or on
hardcopy, and
o a well-developed, simple and effective programming language which
includes conditionals, loops, user-defined recursive functions and input and
output facilities.
The term “environment” is intended to characterize it as a fully planned and
coherent system, rather than an incremental accretion of very specific and
inflexible tools, as is frequently the case with other data analysis software.

R, like S, is designed around a true computer language, and it allows users to add
additional functionality by defining new functions. Much of the system is itself
written in the R dialect of S, which makes it easy for users to follow the
algorithmic choices made. For computationally-intensive tasks, C, C++ and
Fortran code can be linked and called at run time. Advanced users can write C
code to manipulate R objects directly.

Many users think of R as a statistics system. We prefer to think of it as an


environment within which statistical techniques are implemented. R can be
extended (easily) via packages. There are about eight packages supplied with the R

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 39

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
distribution and many more are available through the CRAN family of Internet
sites covering a very wide range of modern statistics.

R has its own LaTeX-like documentation format, which is used to supply


comprehensive documentation, both on-line in a number of formats and in
hardcopy.

FUNCTION

Function
Function Definition
Function Components
Types of Function
Built in function
User defined function
Passing Arguments to Functions
Function Calling
with an arguments
with no arguments
with arguments values ( by position and by names)
with default arguments.

Function

o A set of statements which are organized together to perform a specific


task is known as a function.
o R provides a series of in-built functions, and it allows the user to
create their own functions.

o Functions are used to perform tasks in the modular approach.

 Functions are used to avoid repeating the same task and to reduce
complexity.
 To understand and maintain our code, we logically break it into smaller parts
using the function.

A function should be

 Written to carry out a specified task.


 May or may not have arguments
 Contain a body in which our code is written.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 40

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
 May or may not return one or more output values.

Function Definition

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


function definition is as follows –

Function Components

There are 4 types of function components

1) Arguments
2) Function Name
3) Return Value
4) Function Body

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 41

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Types of Function

R also has two types of function.

Built-in Function
User-defined Function.

In R, there are lots of built-in functions which we can directly call in the program
without defining them.
We can also create and use our own functions referred as user defined functions.

Built-in Function

 The functions which are already created or defined in the programming


framework are known as built-in functions.
 User doesn't need to create these types of functions, and these functions are
built into an application.
 End-users can access these functions by simply calling it.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 42

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
 R have different types of built-in functions such as seq(), mean(), max(), and
sum(x)
 They are directly called by user written programs.

Example

Output

User defined Function

R allows us to create our own function in our program.


A user defines a user-define function to fulfill the requirement of user.
Once these functions are created, we can use these functions like in-built function.

Example

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 43

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
Output

Passing Arguments to Functions in R Programming Language

There are several ways you can pass the arguments to the function:

1. Case 1: Generally in R, the arguments are passed to the function in the same
order as in the function definition.
2. Case 2: If you do not want to follow any order what you can do is you can
pass the arguments using the names of the arguments in any order.
3. Case 3: If the arguments are not passed the default values are used to
execute the function.

Function Calling
with an arguments
with no arguments
with arguments values ( by position and by names)
with default arguments.

Function Calling with an arguments

We can easily call a function by passing an appropriate argument in the function.

Example

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 44

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Output

Function Calling with no arguments

Output

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 45

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Function Calling with arguments values ( by position and by names)

We can supply the arguments to a function call in the same sequence as defined in
the function or can supply in a different sequence but assigned them to the names
of the arguments.

Example

Output

Function calling with default arguments.


Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 46

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

o To get the default result, we assign the value to the arguments in the function
definition, and then we call the function without supplying argument.
o If we pass any argument in the function call, then it will get replaced with
the default value of the argument in the function definition.

Example

Output

Recursion Function

Recursion Function
Features of Recursion Function
Types of Recursion Function
Example Program
Application of Recursion Function

Recursion Function

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 47

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

o Recursion, in the simplest terms, is a type of looping technique. It exploits


the basic working of functions in R.
o Recursion is when the function calls itself.
o This forms a loop, where every time the function is called, it calls itself
again and again and this technique is known as recursion.
o Since the loops increase the memory we use the recursion.
o The recursive function uses the concept of recursion to perform iterative
tasks they call themselves, again and again, which acts as a loop.
o These kinds of functions need a stopping condition so that they can stop
looping continuously.
o Recursive functions call themselves.
o They break down the problem into smaller components.
o The function() calls itself within the original function() on each of the
smaller components.
o After this, the results will be put together to solve the original problem.

Features of Recursion Function

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

Types of Recursion Function

1. Direct Recursion: The recursion that is direct involves a function calling


itself directly. This kind of recursion is the easiest to understand.

2. Indirect Recursion: An indirect recursion is a series of function calls in


which one function calls another, which in turn calls the original function.

3. Mutual Recursion: Multiple functions that call each other repeatedly make
up mutual recursion. To complete a task, each function depends on the
others.

4. Nested Recursion: Nested recursion happens when one recursive function


calls another recursively while passing the output of the first call as an
argument. The arguments of one recursion are nested inside of this one.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 48

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

5. Structural Recursion: Recursion that is based on the structure of the data


is known as structural recursion. It entails segmenting a complicated data
structure into smaller pieces and processing each piece separately.

Example

Output

Applications of Recursion Function

o Recursive functions are used in many efficient programming techniques like


dynamic programming language(DSL) or divide-and-conquer algorithms.
o In dynamic programming, for both top-down as well as bottom-up
approaches, recursion is vital for performance.
o 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.
o 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.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 49

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
Replacement Function
The replace() function in R can be used to replace specific elements in a vector with new
values.

replace(x, list, values)

where:

 x: Name of vector
 list: Elements to replace
 values: Replacement values

Example

Replace one value in a Vector


#define vector of values
data <- c(3, 6, 8, 12, 14, 15, 16, 19, 22)

#define new vector with a different value in position 2


data_new <- replace(data, 2, 50)

#view new vector


data_new

[1] 3 50 8 12 14 15 16 19 22

Replace multiple values in a Vector

R Data Structure

Data Structure

 A data structure is a particular way of organizing data in a computer so that


it can be used effectively.
 The idea is to reduce the space and time complexities of different tasks.
 Data structures in R programming are tools for holding multiple values.
 R’s base data structures are often organized by their dimensionality (1D, 2D,
or nD) and whether they’re homogeneous (all elements must be of the
identical type) or heterogeneous (the elements are often of various types).
 This gives rise to the six data types which are most frequently utilized in
data analysis.

The most essential data structures used in R include:

 Vectors

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 50

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
 Lists
 Dataframes
 Matrices
 Arrays
 Factors

Vectors

o A vector is an ordered collection of basic data types of a given length.


o The only key thing here is all the elements of a vector must be of the
identical data type e.g homogeneous data structures.
o Vectors are one-dimensional data structures.

Example

Output

Lists

o A list is a generic object consisting of an ordered collection of objects.


o Lists are heterogeneous data structures.
o These are also one-dimensional data structures.
o A list can be a list of vectors, list of matrices, a list of characters and a list of
functions and so on.

Example
empId = c(1, 2, 3, 4)

empName = c("Debi", "Sandeep", "Subham", "Shiba")

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 51

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

numberOfEmp = 4

empList = list(empId, empName, numberOfEmp)

print(empList)

Output

Dataframes

Dataframes are generic data objects of R which are used to store the tabular data.
Dataframes are the foremost popular data objects in R programming because we
are comfortable in seeing the data within the tabular form.
They are two-dimensional, heterogeneous data structures.
These are lists of vectors of equal lengths.

Data frames have the following constraints placed upon them:

o A data-frame must have column names and every row should have a
unique name.
o Each column must have the identical number of items.
o Each item in a single column must be of the same data type.
o Different columns may have different data types.

To create a data frame we use the data.frame() function.

Example
Name = c("Amiya", "Raj", "Asish")

Language = c("R", "Python", "Java")

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 52

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Age = c(22, 25, 45)

df = data.frame(Name, Language, Age)

print(df)

Output

Matrices

A matrix is a rectangular arrangement of numbers in rows and columns.


In a matrix, as we know rows are the ones that run horizontally and columns are
the ones that run vertically.
Matrices are two-dimensional, homogeneous data structures.
To create a matrix in R we need to use the function called matrix.
The arguments to this matrix() are the set of elements in the vector.
By default, matrices are in column-wise order.

Example

A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow= TRUE)

print(A)

Output

Arrays

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 53

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
Arrays are the R data objects which store the data in more than two dimensions.
Arrays are n-dimensional data structures.

Example

if we create an array of dimensions (2, 3, 3) then it creates 3 rectangular matrices


each with 2 rows and 3 columns. They are homogeneous data structures.

To create an array in R we need to use the function called array().


The arguments to this array() are the set of elements in vectors.
We have to pass a vector containing the dimensions of the array.
A = array( c(1, 2, 3, 4, 5, 6, 7, 8),dim = c(2, 2, 2) )

print(A)

Output

Classes

Classes and Objects are basic concepts of Object-Oriented Programming that


revolve around the 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.

R has a three-class system.

S3 class
S4 class
Reference Classes.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 54

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

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.
Example

movieList <- list(name = "Iron man", leadActor = "Robert Downey Jr")

class(movieList) <- "movie"

movieList

Output

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.
print(movieList)

Example: Creating a user-defined print function

print.movie <- function(obj)

cat("The name of the movie is", obj$name,".\n")

cat(obj$leadActor, "is the lead actor.\n")

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 55

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

Output

S4 Class

Programmers of other languages like C++, 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.

Example
library(methods)

setClass("movies", slots=list(name="character", leadActor = "character"))

movieList <- new("movies", name="Iron man", leadActor = "Robert Downey Jr")

movieList

Output

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.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 56

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
The following example shows how to create a method:

movieList

Output

setMethod("show", "movies",
function(object)
{
cat("The name of the movie is ", object@name, ".\n")
cat(object@leadActor, "is the lead actor.\n")
}
)
movieList

Output

Reference Class

Reference Class is an improvement over S4 Class. Here the methods belong to the
classes.
Defining a Reference class is similar to defining S4 classes.
We use setRefClass() instead of setClass() and “fields” instead of “slots”.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 57

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
Example
library(methods)

movies <- setRefClass("movies", fields = list(name = "character",

leadActor = "character", rating = "numeric"))

movieList <- movies(name = "Iron Man",

leadActor = "Robert downey Jr", rating = 7)

movieList

Output

INPUT / OUTPUT
INPUT

Developers often have a need to interact with users, either to get data or to provide
some sort of result.
Most programs today use a dialog box as a way of asking the user to provide some
type of input.
Like other programming languages in R it’s also possible to take input from the
user.
There are two methods in R.

Using readline() method


Using scan() method

Using readline() method

In R language readline() method takes input in string format.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 58

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
one inputs an integer then it is input as a string, one wants to input 255, then it will
input as “255”, like a string.
So one needs to convert that input value to the format that he needs.
In this case, string “255” is converted to integer 255.
To convert the inputted value to the desired data type.
There are some functions in R,
 as.integer(n); —> convert to integer
 as.numeric(n); —> convert to numeric type (float, double etc)
 as.complex(n); —> convert to complex number (i.e 3+2i)
 as.Date(n) —> convert to date …, etc

Syntax

var <- readline();


var <- as.integer(var);

Example

var = readline();

var = as.integer(var);

print(var)

Output

Using scan() method

scan() method takes input from the console.


This method is a very handy method while inputs are needed to taken quickly for
any mathematical calculation or for any dataset.
This method reads data in the form of a vector or list.
This method also uses to reads input from a file also.

Syntax

x = scan()

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 59

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V

scan() method is taking input continuously, to terminate the input process, need
to press Enter key 2 times on the console.

Example:

This is simple method to take input using scan() method, where some integer
number is taking as input and print those values in the next line on the console.
x = scan()

print(x)

Output

OUTPUT

Method to print output in R program, there is a function called print() is used.


Also if the program of R is written over the console line by line then the output is
printed normally, no need to use any function for print that output. To do this just
select the output variable and press run button.

Example

x <- "GeeksforGeeks"
x

Output

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 60

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
Print output using print() function

Using print() function to print output is the most common method in R.


Implementation of this method is very simple.

Syntax

print(“any string”) or, print(variable)

Example
print("GFG")

x <- "GeeksforGeeks"
print(x)

Output

STRING MANIPULATIONS

Any value written within a pair of single quote or double quotes in R is treated as a
string.
Internally R stores every string within double quotes, even when you create them
with single quote.

Rules Applied in String Construction

 The quotes at the beginning and end of a string should be both double quotes
or both single quote. They can not be mixed.
 Double quotes can be inserted into a string starting and ending with single
quote.
 Single quote can be inserted into a string starting and ending with double
quotes.
 Double quotes can not be inserted into a string starting and ending with
double quotes.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 61

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
 Single quote can not be inserted into a string starting and ending with single
quote.

Examples of Valid Strings

Following examples clarify the rules about creating a string in R.


a <- 'Start and end with single quote'
print(a)

b <- "Start and end with double quotes"


print(b)

c <- "single quote ' in between double quotes"


print(c)

d <- 'Double quotes " in between single quote'


print(d)

Output

[1] "Start and end with single quote"


[1] "Start and end with double quotes"
[1] "single quote ' in between double quote"
[1] "Double quote \" in between single quote"

Examples of Invalid Strings


e <- 'Mixed quotes"
print(e)

f <- 'Single quote ' inside single quote'


print(f)

g <- "Double quotes " inside double quotes"


print(g)

Output

Error: unexpected symbol in:


"print(e)
f <- 'Single"
Execution halted

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 62

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
String Manipulation

Concatenating Strings - paste() function

Many strings in R are combined using the paste() function. It can take any number
of arguments to be combined together.

Syntax

The basic syntax for paste function is −

paste(..., sep = " ", collapse = NULL)

Following is the description of the parameters used −

o ... represents any number of arguments to be combined.


o sep represents any separator between the arguments. It is optional.
o collapse is used to eliminate the space in between two strings. But not the
space within two words of one string.
Example
a <- "Hello"
b <- 'How'
c <- "are you? "

print(paste(a,b,c))

print(paste(a,b,c, sep = "-"))

print(paste(a,b,c, sep = "", collapse = ""))

Output

[1] "Hello How are you? "


[1] "Hello-How-are you? "
[1] "HelloHoware you? "

Changing the case - toupper() & tolower() functions

These functions change the case of characters of a string.

Syntax

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 63

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
The basic syntax for toupper() & tolower() function is −

toupper(x)

tolower(x)

Following is the description of the parameters used −

x is the vector input.

Example
# Changing to Upper case.
result <- toupper("Changing To Upper")
print(result)

# Changing to lower case.


result <- tolower("Changing To Lower")
print(result)

Output

[1] "CHANGING TO UPPER"


[1] "changing to lower"

Extracting parts of a string - substring() function

This function extracts parts of a String.

Syntax

The basic syntax for substring() function is −

substring(x,first,last)

Following is the description of the parameters used −

x is the character vector input.

first is the position of the first character to be extracted.


last is the position of the last character to be extracted.

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 64

Downloaded by BARATH S (htarab86@gmail.com)


lOMoARcPSD|20574153

DS4015 UNIT - V
Example
# Extract characters from 5th to 7th position.
result <- substring("Extract", 5, 7)
print(result)

Output

[1] "act"

String Length

There are many usesful string functions in R.


For example, to find the number of characters in a string, use the nchar() function:

Example

str <- "Hello World!"

nchar(str)

Output

[1] 12

Check a String

Use the grepl() function to check if a character or a sequence of characters are


present in a string:

Example

str <- "Hello World!"

grepl("H", str)
grepl("Hello", str)
grepl("X", str)

Output
[1] TRUE
[1] TRUE
[1] FALSE

Mrs.K.Vijayalakshmi, MCA., M.Phil.,B.Ed., 65

Downloaded by BARATH S (htarab86@gmail.com)

You might also like