0% found this document useful (0 votes)
13 views

R Programming by Adi

zxzxzxzx

Uploaded by

mdhsrinivasan10
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)
13 views

R Programming by Adi

zxzxzxzx

Uploaded by

mdhsrinivasan10
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/ 50

Introduction

to

PROF. DR. K. ADISESHA


R-Programming 2
Part-1

R-Programming Introduction

Data Objects in R

Variables in R

Operators in R

R Programming
3
Introduction

R Programming:
"R is an interpreted computer programming language which was created by Ross
Ihaka and Robert Gentleman at the University of Auckland, New Zealand."
➢ It is also a software environment used to analyze statistical information, graphical
representation, reporting, and data modeling.
➢ R is the implementation of the S programming language, which is combined with
lexical scoping semantics.
➢ 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.
➢ R allows integration with the procedures written in the C, C++, .Net, Python, and
FORTRAN languages to improve efficiency.
Prof. Dr. K. Adisesha
4
Introduction

History of R Programming:
The history of R goes back about 20-30 years ago. R was developed by Ross lhaka and
Robert Gentleman in the University of Auckland, New Zealand, and the R Development
Core Team currently develops it.
➢ This programming language name is taken from
the name of both the developers.
➢ R communicate with the other languages and
possibly calls Python, Java, C++.
➢ The big data world is also accessible to R. We can
connect R with different databases like Spark or
Hadoop.
Prof. Dr. K. Adisesha
5
Introduction

Features of R programming:
R is a domain-specific programming language which aims to do data analysis. The
most important arguably being the notation of vectors allowing us to perform a
complex operation on a set of values in a single command.
➢ There are the following features of R programming:
❖ R is an interpreted language used as data analysis software.
❖ R supports procedural programming with functions and object-oriented programming
with generic functions.
❖ It has a consistent and incorporated set of tools which are used for data analysis.
❖ It is an open-source, powerful, and highly extensible software.
❖ It provides highly extensible graphical techniques.
❖ It allows us to perform multiple calculations using vectors.
Prof. Dr. K. Adisesha
6
Introduction

Why R programming:
There are various tools available in the market for Data Visualization: R, Power BI,
Spark, Qlikview etc..
➢ R, SAS, and SPSS are three statistical languages. Of these three statistical languages,
R is the only an open source.
➢ Uses of R programming:
❖ R is a programming and statistical language.
❖ R is used for data Analysis and Visualization.
❖ R is simple and easy to learn, read and write.
❖ R is an example of a FLOSS (Free Libre and Open Source Software) where one can
freely
❖ distribute copies of this software, read its source code, modify it, etc.
Prof. Dr. K. Adisesha
7
Introduction

Applications of R:
R programming is used for statistical information and data representation. So it is
required that we should have the knowledge of statistical theory in mathematics.
➢ There are several-applications available in real-time.
❖ Facebook ❖ XBOX ONE
❖ Google ❖ ANZ
❖ Twitter ❖ FDA
❖ HRDAG
❖ Sunlight Foundation
❖ RealClimate
❖ NDAA
Prof. Dr. K. Adisesha
8
Introduction

Installation of R:
R programming is a very popular language and to work on that we have to install two
things, i.e., R and RStudio. R and RStudio works together to create a project on R.
➢ The official site https://cloud.r-project.org provides
binary files for major operating systems including
Windows, Linux, and Mac OS.
➢ First, we have to download the R setup from
https://cloud.r-project.org/bin/windows/base/.
➢ When we click on Download R 3.6.1 for windows,
our downloading will be started of R setup. Once
the downloading is finished, we have to run the
setup
Prof. Dr. of R
K. Adisesha
9
Introduction

Editors for R programming :


The prominent editors available for R programming language are:
➢ RGUI(R graphical user interface) – R-studio – Studio R offers a richer editing
environment than RGUI and makes some common tasks easier and more fun.
➢ RStudio - RStudio is an integrated development environment (IDE) for R language.
Rstudio is a code editor and development environment, with some nice features that
make code development in R easy and fun.
➢ R Command Prompt: Once you have R environment setup, then it’s easy to start your
R command prompt by just clicking on R Software icon. This will launch R interpreter
and you will get a prompt > where you can start typing your programs or commands.

Prof. Dr. K. Adisesha


10
Introduction

Editors for R programming :RStudio is an integrated development environment


(IDE) :

Prof. Dr. K. Adisesha


11
Introduction

Computations in R:
To understand computations in R, two slogans are helpful:
❖ Everything that exists is an object.
❖ Everything that happens is a function call.
➢ Variables, Datatypes in R: Everything in R is an object. R has 5 atomic vector types.
By atomic, we mean the vector only holds data of a single type.
❖ Character : "a", “adi"
❖ Numeric (real or decimal) : 10, 25.5
❖ Integer : 2L (the L tells R to store this as an integer)
❖ Logical : TRUE, FALSE
❖ Complex : 1+4i (complex numbers with real and imaginary parts)
Prof. Dr. K. Adisesha
12
Introduction

Computations in R:
R provides many functions to examine features of vectors and other objects, for
example:
➢ class() - what kind of object is it (high-level)?
➢ typeof() - what is the object’s data type (low-level)?
➢ length() - how long is it? What about two dimensional objects?
➢ attributes() - does it have any metadata?
v = “Adisesha" v = 2L
print(class(v)) print(class(v))
o/pt: [1] "character“ o/p: [1] "integer"

Prof. Dr. K. Adisesha


13
Data Objects in R

Data Objects in R:
Data types are used to store information. In R, we do not need to declare a variable as
some data type.
➢ The variables are assigned with R-Objects and the data type of the R-object becomes
the data type of the variable.
➢ There are mainly six data types present in R:
❖ Vectors
❖ Lists
❖ Matrices
❖ Arrays
❖ Factors
❖ Data Frames
Prof. Dr. K. Adisesha
14
Data Objects in R

Vectors: A Vector is a sequence of data elements of the same basic type.


➢ Scalar variable A scalar is a single number.
➢ Scalar variable with the numeric value 5: x = 5. Vector variable A vector is a sequence
of numbers.
>vtr = c(1, 3, 5 ,7 9) or >vtr <- c (1, 3, 5 ,7 9)
>print(vtr) o/p: [1] 1 3 5 7 9
➢ Creating sequence vector by using colon operator
>v = 2:12
>print(v) o/p: [1] 2 3 4 5 6 7 8 9 10 11 12
➢ Using seq() (sequence) function, create vector from 1 to 9 increment by 2.
>a=seq(1,10,by=2)
>a o/p: [1] 1 3 5 7 9
Prof. Dr. K. Adisesha
15
Data Objects in R

List: Lists are the R objects which contain elements of different types like − numbers,
strings, vectors and another list inside it.
➢ A list can also contain a matrix or a function as its elements. List is created using list()
function.
>n = c(1, 2, 3, 4, 5) O/p –
>s = c("adi", “sunny", “prajwal") [[1]]
>x = list(n, s, TRUE) [1] 1 2 3 4 5
>x [[2]]
[1] "adi", “sunny", “prajwal"
[[3]]
[1] TRUE

Prof. Dr. K. Adisesha


16
Data Objects in R

Matrices: Matrices are the R objects in which the elements are arranged in a two-
dimensional rectangular layout. A Matrix is created using the matrix() function.
➢ A list can also contain a matrix or a function as its elements. List is created using list()
function.
➢ Example: matrix (data, nrow, ncol, byrow, dimnames) where,
❖ data is the input vector which becomes the data elements of the matrix.
❖ nrow is the number of rows to be created.
❖ ncol is the number of columns to be created.
❖ byrow is a logical clue. If TRUE then the input vector elements are arranged by row.
❖ dimname is the names assigned to the rows and columns. o/p: [,1] [,2] [,3]
[1,] "a" "a" "b"
>M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
[2,] "c" "b" "a"
>print(M)
Prof. Dr. K. Adisesha
17
Data Objects in R

Arrays: Arrays are the R data objects which can store data in more than two-
dimensions.
➢ For example − If we create an array of dimension (2, 3, 4) then it creates 4 rectangular
matrices each with 2 rows and 3 columns. While matrices are confined to two
dimensions, arrays can be of any number of dimensions.
➢ An array is created using the array() function. It takes vectors as input and uses the
values in the dim parameter to create an array.
➢ Example: Here we create two arrays with two elements which are 3x3 matrices each
>v1 <- c(5,9,3) Output – , , 1 Output – , , 2
>v2 <- c(10,11,12,13,14,15) [,1] [,2] [,3] [,1] [,2] [,3]
[1,] 5 10 13 [1,] 5 10 13
>result<- array(c(v1,v2),dim = c(3,3,2))
[2,] 9 11 14 [2,] 9 11 14
>result [3,] 3 12 15 [3,] 3 12 15
Prof. Dr. K. Adisesha
18
Data Objects in R

Factors: Factors are the data objects which are used to categorize the data and store it
as levels. They can store both strings and integers.
➢ They are useful in data analysis for statistical modeling.
➢ Factors are created using the factor() function. The n-levels functions gives the count of
levels.
➢ Example: # Create a factor object from vector:
apple_colors<- c('green','green','yellow','red','red','red','green')
# Create a factor object.
factor_apple<- factor(apple_colors) Output –
# Print the factor. [1] green green yellow red red red green
print(factor_apple) Levels: green red yellow
print(nlevels(factor_apple)) [1] 3
Prof. Dr. K. Adisesha
19
Data Objects in R

Data Frames: A data frame is a table or a two-dimensional array-like structure in


which each column contains values of one variable and each row contains one set of
values from each column.
➢ Data frames are tabular data objects. Unlike a matrix in data frame each column can
contain different modes of data.
➢ The first column can be numeric while the second column can be character and third
column can be logical. It is a list of vectors of equal length.
➢ Data Frames are created using the data.frame() function.
➢ Example: Create the data frame. Output –
BMI <- data.frame( gender = c("Male", "Male", "Female"), gender height weight Age
height = c(152, 171.5, 165), 1 Male 152.0 81 42
weight = c(81,93, 78), 2 Male 171.5 93 38
Prof. Dr. K. Adisesha Age = c(42,38,26) ) 3 Female 165.0 78 26
20
Variables in R

Variables: A variable provides us with named storage that our programs can
manipulate.
➢ A variable in R can store an atomic vector, group of atomic vectors or a combination of
many R objects.
➢ A valid variable name consists of letters, numbers and the dot or underline characters.
➢ The variable name starts with a letter or the dot not followed by a number.
➢ The variables can be assigned values using leftward, rightward and equal to operator.
➢ The values of the variables can be printed using print() or cat()function.
➢ Example: var_name2. is valid it has letters, numbers, dot and underscore.
var_name% is Invalid it has the character '%'. Only dot(.) and underscore allowed.
# Assignment using equal operator. var.1 = c(0,1,2,3)
# Assignment using leftward operator. var.2 <- c("learn","R")
# K.Assignment
Prof. Dr. Adisesha using rightward operator. c(TRUE,1) -> var.3
21
Variables in R

Data Type of a Variable: In R, a variable itself is not declared of any data type,
rather it gets the data type of the R - object assigned to it.
➢ So R is called a dynamically typed language, which means that we can change a
variable’s data type of the same variable again and again when using it in a program.
➢ To know all the variables currently available in the workspace we use the ls() function.
print(ls())
➢ Variables can be deleted by using the rm() function. rm(var.1)
➢ Example:
> var_x<- "Hello"
> cat("The class of var_x is ",class(var_x),"\n")
> var_x<- 34.5
> cat(" Now the class of var_x is ",class(var_x),"\n")
Prof. Dr. K. Adisesha
22
Keywords in R

R – Keywords: Keywords are specific reserved words in R, each of which has a specific
feature associated with it.
➢ Almost all of the words which help one to use the functionality of the R language are
included in the list of keywords.
➢ In R, one can view these keywords by using either help(reserved) or ?reserved.
➢ Example: if in NaN
else next NA
while break NA_integer
repeat TRUE NA_real
for FALSE NA_complex_
function NULL NA_character_
Inf
Prof. Dr. K. Adisesha
23
Operators in R

Operators: An operator is a symbol that tells the compiler to perform specific


mathematical or logical manipulations.
➢ R language is rich in built-in operators and provides following types of operators
➢ Types of Operators in R programming −
❖ Arithmetic Operators
❖ Relational Operators
❖ Logical Operators
❖ Assignment Operators
❖ Miscellaneous Operators
Prof. Dr. K. Adisesha
24
Operators in R

Arithmetic Operators: Arithmetic operations in R simulate various math operations,


like addition, subtraction, multiplication, division, and modulo using the specified
operator between operands
➢ The R operators are performed element-wise at the corresponding positions of the
vectors. −
❖ Addition operator (+): The values at the corresponding positions of both operands are added.
❖ Subtraction Operator (-): The second operand values are subtracted from the first.
❖ Multiplication Operator (*): The multiplication of corresponding elements of vectors and Integers are
multiplied with the use of the ‘*’ operator.
❖ Division Operator (/): The first operand is divided by the second operand with the use of the ‘/’ operator.
❖ Power Operator (^): The first operand is raised to the power of the second operand.
❖ Modulo Operator (%%): The remainder of the first operand divided by the second operand is returned.
Prof. Dr. K. Adisesha
25
Operators in R

Arithmetic Operators: Arithmetic operations in R simulate various math operations,


like addition, subtraction, multiplication, division, and modulo using the specified
operator between operands
➢ Example −
# R program to illustrate the use of Arithmetic operators
vec1 <- c(0, 2)
vec2 <- c(2, 3)
# Performing operations on Operands Output
cat ("Addition of vectors :", vec1 + vec2, "\n") Addition of vectors : 2 5
cat ("Subtraction of vectors :", vec1 - vec2, "\n") Subtraction of vectors : -2 -1
cat ("Multiplication of vectors :", vec1 * vec2, "\n") Multiplication of vectors : 0 6
cat ("Division of vectors :", vec1 / vec2, "\n") Division of vectors : 0 0.6666667
cat ("Modulo of vectors :", vec1 %% vec2, "\n") Modulo of vectors : 0 2
cat
Prof. Dr. ("Power operator :", vec1 ^ vec2)
K. Adisesha Power operator : 0 8
26
Operators in R

Logical Operators: Logical operations in R simulate element-wise decision operations,


based on the specified operator between the operands, which are then evaluated to either
a True or False boolean value.
➢ Any non-zero integer value is considered as a TRUE value, be it a complex or real
number.
❖ Logical AND operator (&): Returns True if both the operands are True.
❖ Logical OR operator (|): Returns True if either of the operands is True.
❖ NOT operator (!): A unary operator that negates the status of the elements of the operand.
❖ Logical AND operator (&&): Returns True if both the first elements of the operands are
True.
❖ Logical OR operator (||): Returns True if either of the first elements of the operands is True.
Prof. Dr. K. Adisesha
27
Operators in R

Logical Operators: Logical operations in R simulate element-wise decision operations,


based on the specified operator between the operands, which are then evaluated to either
a True or False boolean value.
➢ Example −The following R code illustrates the usage of all Logical Operators in R
# the use of Logical operators
vec1 <- c(0,2)
vec2 <- c(TRUE,FALSE)
Output
# Performing operations on Operands
Element wise AND : FALSE FALSE
cat ("Element wise AND :", vec1 & vec2, "\n")
Element wise OR : TRUE TRUE
cat ("Element wise OR :", vec1 | vec2, "\n")
Logical AND : FALSE
cat ("Logical AND :", vec1 && vec2, "\n")
Logical OR : TRUE
cat ("Logical OR :", vec1 || vec2, "\n")
Negation : TRUE FALSE
cat ("Negation :", !vec1))
Prof. Dr. K. Adisesha
28
Operators in R

Relational Operators: The relational operators in R carry out comparison operations


between the corresponding elements of the operands.
➢ Returns a Boolean TRUE value if the first operand satisfies the relation compared to the second.
A TRUE value is always considered to be greater than the FALSE.
❖ Less than (<): Returns TRUE if the corresponding element of the first operand is less than that of the
second operand. Else returns FALSE.
❖ Less than equal to (<=): Returns TRUE if the corresponding element of the first operand is less
than or equal to that of the second operand. Else returns FALSE.
❖ Greater than (>): Returns TRUE if the corresponding element of the first operand is greater than
that of the second operand. Else returns FALSE.
❖ Greater than equal to (>=): Returns TRUE if the corresponding element of the first operand is
greater or equal to that of the second operand. Else returns FALSE.
❖ Not equal to (!=): Returns TRUE if the corresponding element of the first operand is not equal to the
Prof. Dr. K. Adisesha
second operand. Else returns FALSE
29
Operators in R

Assignment Operators: Assignment operators in R are used to assigning values to


various data objects in R. The objects may be integers, vectors, or functions. These values
are then stored by the assigned variable names.
➢ There are two kinds of assignment operators: Left and Right.
❖ Left Assignment (<- or <<- or =): Assigns a value to a vector.
vec1 = c("ab", TRUE)
print (vec1)
Output : "ab" "TRUE"
❖ Right Assignment (-> or ->>): Assigns value to a vector.
c("ab", TRUE) ->> vec1
print (vec1)
Output : "ab" "TRUE"
Prof. Dr. K. Adisesha
30
Operators in R

Assignment Operators: : Assignment operators in R are used to assigning values to


various data objects in R. The objects may be integers, vectors, or functions.
➢ Example −The following R code illustrates the usage of all Assignment operators:
vec1 <- c(2:5)
c(2:5) ->> vec2
vec3 <<- c(2:5)
vec4 = c(2:5) Output
c(2:5) -> vec5 vector 1 : 2 3 4 5
cat ("vector 1 :", vec1, "\n") vector 2 : 2 3 4 5
cat("vector 2 :", vec2, "\n") vector 3 : 2 3 4 5
cat ("vector 3 :", vec3, "\n") vector 4 : 2 3 4 5
cat("vector 4 :", vec4, "\n") vector 5 : 2 3 4 5
cat("vector 5 :", vec5)
Prof. Dr. K. Adisesha
31
Operators in R

Miscellaneous Operators: These are the mixed operators in R that simulate the
printing of sequences and assignment of vectors, either left or right-handed.
➢ There are two kinds of Miscellaneous Operators:
❖ %in% Operator: Checks if an element belongs to a list and returns a boolean value TRUE if
the value is present else FALSE.
val <- 0.1
list1 <- c(TRUE, 0.1,"apple")
print (val %in% list1)
Output : TRUE Checks for the value 0.1 in the specified list. It exists, therefore, prints TRUE.
❖ %*% Operator: This operator is used to multiply a matrix with its transpose. Transpose of
the matrix is obtained by interchanging the rows to columns and columns to rows.
pro = mat %*% t(mat)
Prof. Dr. K. Adisesha
print(pro)
32
Operators in R

Miscellaneous Operators: These are the mixed operators in R that simulate the
printing of sequences and assignment of vectors, either left or right-handed.
❖ %*% Operator: This operator is used to multiply a matrix with its transpose. Transpose of
the matrix is obtained by interchanging the rows to columns and columns to rows.
# R program to illustrate the use of Miscellaneous operators
mat <- matrix (1:4, nrow = 1, ncol = 4) Output
print("Matrix elements using : ") [1] "Matrix elements using : "
print(mat) [,1] [,2] [,3] [,4]
product = mat %*% t(mat) [1,] 1 2 3 4
print("Product of matrices")
[1] "Product of matrices"
print(product,) [,1]
cat ("does 1 exist in prod matrix :", "1" %in% product) [1,] 30
Prof. Dr. K. Adisesha
does 1 exist in prod matrix : FALSE
33
Control Statements

Control Statements in R Programming: Control statements are expressions used to


control the execution and flow of the program based on the conditions provided in the
statements.
➢ if condition: This control structure checks the expression provided in parenthesis is true
or not. If true, the execution of the statements in braces {} continues.
➢ Syntax: Example:
if(expression) x <- 100
{ statements if(x > 10){
print(paste(x, "is greater than 10"))
.... }
....
} Output:
[1] "100 is greater than 10"
Prof. Dr. K. Adisesha
34
Control Statements

Control Statements in R Programming: Control statements are expressions used to


control the execution and flow of the program based on the conditions provided in the
statements.
➢ if-else condition: It is similar to if condition but when the test expression in if condition
fails, then statements in else condition are executed.
➢ Syntax: Example: Check value is less than or greater than 10
if(expression){ x <- 5
statements if(x > 10){
.... } print(paste(x, "is greater than 10"))
}else{
else{ statements
print(paste(x, "is less than 10"))
.... }
.... } Output:
Prof. Dr. K. Adisesha [1] "5 is less than 10"
35
Control Statements

Control Statements in R Programming: Control statements are expressions used to


control the execution and flow of the program based on the conditions provided in the
statements.
➢ for loop: It is a type of loop or sequence of statements executed repeatedly until exit
condition is reached.. Example: Print letters range from 4 to 10
➢ Syntax: x <- letters[4:10]
for(i in x){ print(i) }
for(value in vector){
Output:
statements [1] "d"
.... [1] "e"
.... [1] "f"
} [1] "g"
[1] "h"
[1] "i"
Prof. Dr. K. Adisesha
[1] "j"
36
Control Statements

Control Statements in R Programming: Control statements are expressions used to


control the execution and flow of the program based on the conditions provided in the
statements.
➢ Nested loops: Nested loops are similar to simple loops. Nested means loops inside loop.
Moreover, nested loops are used to manipulate the matrix...
➢ Syntax: Output:
# Defining matrix [1] 2 [1] 3
m <- matrix(2:15, 2) [1] 4 [1] 5
[1] 6 [1] 7
for (r in seq(nrow(m))) {
[1] 8 [1] 9
for (c in seq(ncol(m))) { [1] 10 [1] 11
print(m[r, c]) [1] 12 [1] 13
} [1] 14 [1] 15
}
Prof. Dr. K. Adisesha
37
Control Statements

Control Statements in R Programming: Control statements are expressions used to


control the execution and flow of the program based on the conditions provided in the
statements.
➢ while loop: while loop is another kind of loop iterated until a condition is satisfied. The
testing expression is checked first before executing the body of loop.
➢ Syntax:
while(expression){ Example: x = 1 Output: [1] 1
statement # Print 1 to 5 [1] 2
.... while(x <= 5){ [1] 3
.... print(x) [1] 4
x=x+1} [1] 5"
}

Prof. Dr. K. Adisesha


38
Control Statements

Control Statements in R Programming: Control statements are expressions used to


control the execution and flow of the program based on the conditions provided in the
statements.
➢ repeat loop and break statement: repeat is a loop which can be iterated many number of
times but there is no exit condition to come out from the loop.
➢ So, break statement is used to exit from the loop. break statement can be used in any type
of loop to exit from the loop. Example: x = 1 Output:
➢ Syntax: # Print 1 to 5 [1] 1
repeat{ [1] 2
repeat { statements
print(x) [1] 3
.... x=x+1 [1] 4
if(expression) { if(x > 5){ [1] 5
break break
} }
Prof. Dr. K. Adisesha } }
39
Control Statements

Control Statements in R Programming: Control statements are expressions used to


control the execution and flow of the program based on the conditions provided in the
statements.
➢ return statement: return statement is used to return the result of an executed function and
returns control to the calling function.
➢ Syntax: Example: func(1)
return(expression) func <- function(x){ func(0)
if(x > 0){ func(-1)
return("Positive")
}else if(x < 0){ Output:
return("Negative") [1] "Positive"
}else{ [1] "Zero"
return("Zero") [1] "Negative"
Prof. Dr. K. Adisesha } }
40
Control Statements

Control Statements in R Programming: Control statements are expressions used to


control the execution and flow of the program based on the conditions provided in the
statements.
➢ next statement: next statement is used to skip the current iteration without executing the
further statements and continues the next iteration cycle without terminating the loop.
➢ Example: # Defining vector
x <- 1:10 Output:
# Print even numbers [1] 2
for(i in x){ [1] 4
if(i%%2 != 0){ [1] 6
next #Jumps to next loop [1] 8
} print(i) [1] 10
Prof. Dr. K. Adisesha
}
41
R Programming

Classes in R Programming:
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.
➢ Unlike most other programming languages, R has a three-class system.
❖ S3 Classes
❖ S4 Classes
❖ Reference Classes

Prof. Dr. K. Adisesha


42
R Programming

Classes in R Programming:
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.
➢ In S3 systems, methods don’t belong to the class. They belong to generic functions.
Example:
# create a list with required components
Course <- list(name = “K. Adi", Dept = “Computers") Output:
$name
# give a name to your class [1] “K. Adi"
class(BCA) <- Course $Dept
print(BCA) [1] “Computers"
Prof. Dr. K. Adisesha
43
R Programming

Classes in R Programming:
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.
➢ setClass() is used to define a class and new() is used to create the objects.
➢ Example: Output:
An object of class “Course"
library(methods) # definition of S4 class
Slot "name":
setClass(“Course", slots=list(name="character", Subject = "character"))
[1] “Adi"
# creating an object using new() by passing class name and slot values
Slot “Subject":
Course <- new(“Dept", name=“Adi", Subject = “R Programming")
Prof. Dr. K. Adisesha
[1] “R Programming"
44
R Programming

Classes in R Programming:
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”.
Example: library(methods)
# setRefClass returns a generator Output:
Course <- setRefClass(“Course", fields = list(name = "character", Reference class object of class “Course"
Subject = "character",)) Field "name":
#now we can use the generator to create objects [1] “Adi"
Field “Subject":
Dept <- Course(name = “Adi", Subject = “R Pgm", )
[1] “R Prog"
Dept
Prof. Dr. K. Adisesha
45
R Programming

Input from User in R Programming:


Developers often have a need to interact with users, either to get data or to provide some
sort of result.
➢ R two methods to take input from the user.
❖ Using readline() method
❖ Using scan() method.
➢ readline() method: In R language readline() method takes input in string format. If one
inputs an integer then it is inputted as a string.
➢ 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)
Prof. Dr. K. Adisesha
❖ as.Date(n) —> convert to date …, etc
46
R Programming

Input from User in R Programming:


Developers often have a need to interact with users, either to get data or to provide some
sort of result.
➢ scan() method: This 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()
scan() method is taking input continuously, to terminate the input process, need to press Enter
key 2 times on the console.
Prof. Dr. K. Adisesha
47
R Programming

Printing Output of an R Program:


In R there are various methods to print the output. Most common method to print output
in R program, 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.
❖ Print output using print() function.
Syntax: print(“any string”) or, print(variable)
❖ Print output using paste() function inside print() function.
Syntax: print(paste(“any string”, variable)) or, print(paste0(variable, “any string”))
❖ Print output using sprintf() function.
Syntax: sprintf(“any string %d”, variable) or, sprintf(“any string %s”, variable)
❖ Print output using cat() function.
Prof. Dr. K. Adisesha
Syntax: cat(“any string”) or, cat(“any string”, variable)
48
R Programming

Data Visualization in R:
Data visualization is the technique used to deliver insights in data using visual cues such
as graphs, charts, maps, and many others.
➢ This is useful as it helps in intuitive and easy understanding of the large quantities of
data and thereby make better decisions regarding it.
➢ Some of the various types of visualizations offered by R are:
❖ R Plotting: The plot() function is used to draw points (markers) in a diagram.
❖ R Line: To create a line, use the plot() function and add the type parameter with a value "l":
❖ R Scatter Plot: A "scatter plot" is a type of plot used to display the relationship between two
numerical variables, and plots one dot for each observation along x-axis and y-axis.
❖ R Pie Charts: A pie chart is a circular graphical view of data using the pie() function.
❖ R Bar Charts: A bar chart uses rectangular bars to visualize data using the barplot() func.
Prof. Dr. K. Adisesha
49
R Programming

Data Visualization in R:
Data visualization is the technique used to deliver insights in data using visual cues such
as graphs, charts, maps, and many others.

Prof. Dr. K. Adisesha


50
Discussion

Queries ?
Prof. K. Adisesha
9449081542

Prof. Dr. K. Adisesha

You might also like