Unit V
Unit V
Unit V
DS4015 UNIT - V
UNIT - V R LANGUAGE
( 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
History of R Language
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
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
DS4015 UNIT - V
Applications of R Language
( II ) PROGRAMMING STRUCTURES
Command Prompt
R Script
Comments
Keywords
Data Types
Variables
Command Prompt
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
To execute this file in Windows and other operating systems, the process will
remain the same as mentioned below.
Comments
o Code Readability
o Explanation of the code or Metadata of the project
o Prevent execution of code
o To include resources
DS4015 UNIT - V
Types of Comments
Keywords
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
DS4015 UNIT - V
Example
[1] "Sally"
DS4015 UNIT - V
DS4015 UNIT - V
Variables in R
Example
To use a function to print/output variables in R. Just type the name of the variable.
Concatenate Elements
DS4015 UNIT - V
paste(text1, text2)
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!
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
DS4015 UNIT - V
( III ) OPERATORS
Types of Operators
1) Arithmetic Operators
2) Relational Operators
3) Logical Operators
4) Assignment Operators
5) Miscellaneous Operators
DS4015 UNIT - V
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.
DS4015 UNIT - V
DS4015 UNIT - V
DS4015 UNIT - V
DS4015 UNIT - V
4) Assignment Operator
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.
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.
DS4015 UNIT - V
If statement
If-Else statement
Nested If-else statement
If-Else If ladder
Switch statement
DS4015 UNIT - V
if Statement
Flow Diagram
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
DS4015 UNIT - V
if-else Statement
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
DS4015 UNIT - V
Example
Output
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.
DS4015 UNIT - V
Flow Diagram
DS4015 UNIT - V
Example
Output
Nested if Statement
DS4015 UNIT - V
Syntax
Flow Chart
DS4015 UNIT - V
Example
Output
Switch Statement
DS4015 UNIT - V
Syntax
Flow Diagram
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
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
DS4015 UNIT - V
Output
repeat statement.
while statement.
for loop statement.
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.
Syntax
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.
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
DS4015 UNIT - V
Output
While Loop
The While loop executes the same code again and again until a stop
condition is met.
Syntax
DS4015 UNIT - V
Flow Chart
Example
Output
DS4015 UNIT - V
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.
DS4015 UNIT - V
Syntax
break;
Flow Chart
Example
Output
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
DS4015 UNIT - V
Output
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.
DS4015 UNIT - V
distribution and many more are available through the CRAN family of Internet
sites covering a very wide range of modern statistics.
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
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
DS4015 UNIT - V
May or may not return one or more output values.
Function Definition
Function Components
1) Arguments
2) Function Name
3) Return Value
4) Function Body
DS4015 UNIT - V
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
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
Example
DS4015 UNIT - V
Output
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.
Example
DS4015 UNIT - V
Output
Output
DS4015 UNIT - V
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
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
DS4015 UNIT - V
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.
3. Mutual Recursion: Multiple functions that call each other repeatedly make
up mutual recursion. To complete a task, each function depends on the
others.
DS4015 UNIT - V
Example
Output
DS4015 UNIT - V
Replacement Function
The replace() function in R can be used to replace specific elements in a vector with new
values.
where:
x: Name of vector
list: Elements to replace
values: Replacement values
Example
[1] 3 50 8 12 14 15 16 19 22
R Data Structure
Data Structure
Vectors
DS4015 UNIT - V
Lists
Dataframes
Matrices
Arrays
Factors
Vectors
Example
Output
Lists
Example
empId = c(1, 2, 3, 4)
DS4015 UNIT - V
numberOfEmp = 4
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.
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.
Example
Name = c("Amiya", "Raj", "Asish")
DS4015 UNIT - V
print(df)
Output
Matrices
Example
print(A)
Output
Arrays
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
print(A)
Output
Classes
S3 class
S4 class
Reference Classes.
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
Output
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)
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.
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”.
DS4015 UNIT - V
Example
library(methods)
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.
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
Example
var = readline();
var = as.integer(var);
print(var)
Output
Syntax
x = scan()
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
Example
x <- "GeeksforGeeks"
x
Output
DS4015 UNIT - V
Print output using print() function
Syntax
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.
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.
DS4015 UNIT - V
Single quote can not be inserted into a string starting and ending with single
quote.
Output
Output
DS4015 UNIT - V
String Manipulation
Many strings in R are combined using the paste() function. It can take any number
of arguments to be combined together.
Syntax
print(paste(a,b,c))
Output
Syntax
DS4015 UNIT - V
The basic syntax for toupper() & tolower() function is −
toupper(x)
tolower(x)
Example
# Changing to Upper case.
result <- toupper("Changing To Upper")
print(result)
Output
Syntax
substring(x,first,last)
DS4015 UNIT - V
Example
# Extract characters from 5th to 7th position.
result <- substring("Extract", 5, 7)
print(result)
Output
[1] "act"
String Length
Example
nchar(str)
Output
[1] 12
Check a String
Example
grepl("H", str)
grepl("Hello", str)
grepl("X", str)
Output
[1] TRUE
[1] TRUE
[1] FALSE