Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
44 views42 pages

R Programming Basics and Features

Uploaded by

mihirvacharyaa
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)
44 views42 pages

R Programming Basics and Features

Uploaded by

mihirvacharyaa
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

Statistical Computing & R Programming 2025

UNIT I
Introduction of the language, numeric, arithmetic, assignment, and vectors, Matrices and Arrays,
Non-numeric Values, Lists and Data Frames, Special Values, Classes, and Coercion, Basic
Plotting.

Introduction of the language


R programming is an interpreted programming language widely used to analyses statistical
information and a graphical representation. It is also a software environment used to analyse
statistical information, graphical representation, reporting, and data modelling. R programming
language was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New
Zealand

Features of R programming
1. It is a simple and effective programming language which has been well developed.
2. It is a well-designed, easy, and effective language which has the concepts of user-defined,
looping, conditional, and various I/O facilities.
3. For different types of calculation on arrays, lists and vectors, R contains asuite of operators.
4. It provides effective data handling and storage facility.
5. It is an open-source, powerful, and highly extensible software.
6. It provides highly extensible graphical techniques.
7. R is an interpreted language.

R Advantages and Disadvantages

R is the most popular programming language for statistical modelling and analysis. Like other
programming languages, R also has some advantages and disadvantage

Advantages:
1. Open Source and Free: R is free to download, use, and modify, making it accessible to
individuals, academics, and organizations.
2. Excellent for Data Analysis and Statistics: R was built specifically for statistical computing
and data analysis. It has a vast library of packages for various statistical techniques.
3. Rich Package Ecosystem: CRAN (Comprehensive R Archive Network) hosts thousands of
packages for machine learning, data visualization, time-series analysis, etc.
4. Strong Data Visualization Tools: Packages like ggplot2, lattice, and plotly provide powerful
and customizable data visualization capabilities.
5. Active Community and Support: R has a large, active community that contributes to
packages, tutorials, forums (like Stack Overflow), and documentation.
6. Integration with Other Languages and Tools: R can interface with C/C++, Python, Java, and
databases (e.g., SQL), and can be used within Jupyter, RStudio, and Shiny apps.
7. Reproducible Research: Tools like R Markdown and Knitr support dynamic report generation
and reproducible research workflows.

Dept. of CS 1
Statistical Computing & R Programming 2025

Disadvantages
1. Slower Performance: R can be slower than other languages like Python or C++, especially
with large datasets or complex loops.
2. Memory Management Issues: R stores objects in memory (RAM), which can cause problems
with very large datasets unless optimized.
3. Less Friendly for General-Purpose Programming: While R is great for statistical tasks, it's
less intuitive for tasks like web development or software engineering compared to Python or
Java.
4. Steep Learning Curve for Beginners: The syntax and programming style can be challenging
for those new to programming or coming from other languages.
5. Inconsistent Syntax Across Packages: Different packages may use slightly different syntaxes
or conventions, leading to confusion and a lack of standardization.
6. Limited GUI Development Support: While R has tools like Shiny for web apps, it's not ideal
for developing complex desktop or mobile applications.

Data Types in R Programming


➢ R data types are used in computer programming to specify the kind of data that can be stored
in a variable.
➢ The operating system allocates memory based on the data type of the variable and decides what can
be stored in the reserved memory.
➢ There are the following data types which are used in R programming:

Datatype Example Description


Logical True, False It is a special data type for data with only two possible
values which can be construed as true/false.
Numeric 12,32,112,5432 Decimal value is called numeric in R, and it is the default
computational data type.
Integer 3L, 66L, 2346L Here, L tells R to store the value as an integer,
Complex Z=1+2i, t=7+3i A complex value in R is defined as the pure imaginary
value i.
Character In R programming, a character is used to represent string
'a', '"good'",
values. We convert objects into character values with the
"TRUE", '35.4' help of [Link]() function.
Raw To save and work with data at the byte level in R, use the
"Hello" is stored as
raw data type. By displaying a series of unprocessed
48 65 6c 6c 6f bytes, it enables low-level operations on binary data.

Variables

➢ Variables are used to store the information to be manipulated and referenced in the R program.
➢ The R variable can store an atomic vector, a group of atomic vectors, or a combination
of many R objects.
➢ R supports three ways of variable assignment:

▪ Using equal operator: operators use an arrow or an equal sign to assign values to variables.
variable_name = value
Ex: var1 = "hello"
print(var1)

Dept. of CS 2
Statistical Computing & R Programming 2025

▪ Using the leftward operator: data is copied from right to left.


variable_name <- value
Ex: var2 <- "hello"
print(var2)

▪ Using the rightward operator: data is copied from left to right.


value -> variable_name
Ex: "hello" -> var3
print(var3)

The following rules need to be kept in mind while naming a R variable:


1) A valid variable name consists of a combination of alphabets, numbers, dot(.), and
underscore (_) characters. Example: var.1_ is valid.
2) Apart from the dot and underscore operators, no other special character isallowed. Example:
var$1 or var#1 both are invalid
3) Variables can start with alphabets or dot characters. Example: .var or varis valid
4) The variable should not start with numbers or underscore. Example: 2varor _var is invalid.
5) If a variable starts with a dot the next thing after the dot cannot be a [Link]: .3var is
invalid
6) The variable name should not be a reserved keyword in R. Example:TRUE, FALSE,
etc.

R provides some useful methods to perform operations on variables. These methods are used to
determine the data type of the variable, finding a variable, deleting a variable, etc. Following are some
of the methods used to work on variables:

1. class() function
This built-in function is used to determine the data type of the variable provided to it. The R variable
to be checked is passed to this as an argument and it prints the data type in return.
Syntax: class(variable)

Example: var1 = "hello"


print(class(var1))

Output: [1] "character"

2. ls() function
This built-in function is used to know all the present variables in the workspace. This is generally
helpful when dealing with a large number of variables at once and helps prevents overwriting any of
them.
Syntax: ls()

Example: # using equal to operator


var1 = "hello"
# using leftward operator
var2 <- "hello"
# using rightward operator
"hello" -> var3
print(ls())

Output: [1] "var1" "var2" "var3"


3. rm() function

Dept. of CS 3
Statistical Computing & R Programming 2025

This is again a built-in function used to delete an unwanted variable within your [Link] helps
clear the memory space allocated to certain variables that are not in use thereby creating more space
for others. The name of the variable to be deleted is passed as an argument to it.
Syntax: rm(variable)
Example: "hello" -> var3
# Removing variable
rm(var3)
print(var3)

Scope of Variables in R programming


The location where we can find a variable and also access it if required is called the scope of a variable.
There are mainly two types of variable scopes:
1. Global Variables
Global variables are those variables that exist throughout the execution of a program. It can be changed
and accessed from any part of the program.
2. Local Variables
Local variables are those variables that exist only within a certain part of a program like a function and
are released when the function call ends. Local variables do not exist outside the block in which they
are declared, i.e. they cannot be accessed or used outside that block.

Operators in R
An operator is a symbol which tells the compiler to perform specific logical or
mathematical manipulations. In R programming, there are different types of operators, and each
operator performs a different task.

There are the following types of operators used in R:


1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Assignment Operators
5. Miscellaneous Operators
Arithmetic Operators: Arithmetic operators are the symbols which are used to represent
arithmetic mathoperations. There are various arithmetic operators which are supported by
R.
[Link] OPERATOR DESCRIPTION EXAMPLE
1 + This operator isused to add two vectors in R. a<-c(2,3.3,4)
b<-c(11,5,3)
print(a+b)
Output:
[1] 13.0 8.3 7.0
2 - This operator isused to subtract two vectors a<-c(2,3.3,4)
witheach other. b<-c(11,5,3)
print(a-b)
Output:
[1] -11.0 -1.7 1.0
3 * This operator isused to multiply two vectors a<c(2,3.3,4)
witheach other. b<c(11,5,3)
print(a*b)
Output:

Dept. of CS 4
Statistical Computing & R Programming 2025

[1] 22.0 16.5 12.0


4 / This operatordivides the vector from another a<-c(2,3.3,4)
one. b<-c(11,5,3)
print(a/b)
Output:
[1]0.1818182
[2]0.6600000
[3]1.3333333
5 %% This operator isused to find theremainder of a <- c(2, 3.3, 4)
the b <- c(11, 5, 3)
print(a%%b)
output:
[1] 2.0 3.3
1.0
6 %/% This operator is used to find the division of a <- c(2, 3.3, 4)
the first vector with the second(quotient). b <- c(11, 5, 3)
print(a%/%b)
output:
[1] 0 0 1
7 ^ This operator raised the first vector to the a <- c(2, 3.3, 4)
exponent of the second vector. b <- c(11, 5, 3)
print(a^b)
output:
[1] 2048.0000
391.3539
64.0000

Relational Operators: A relational operator compares each element of the first vector with the
corresponding element of the second vector. The result of the comparison will bea Boolean value. There
are the following relational operators which are supported by R:
[Link] OPERATOR DESCRIPTION EXAMPLE
1 > This operator will return TRUE when every a <- c(1, 3, 5)
element inthe first vector is greater thanthe b <- c(2, 4, 6)
corresponding element ofthe second vector. print(a>b)
Output:
[1] FALSE FALSE FALSE
2 < This operator will return TRUE when every a <- c(1, 9, 5)
element inthe first vector is less than the b <- c(2, 4, 6)
corresponding element of thesecond vector. print(a<b)
Output:
[1] TRUE FALSE TRUE
3 <= This operator will return TRUE when every a <- c(1, 9, 5)
element inthe first vector is less than or b <- c(2, 4, 6)
equal to the corresponding element of print(a<=b)
another vector. Output:
[1] TRUE FALSE TRUE
4 >= This operator will return TRUE when every a <- c(1, 9, 5)
element inthe first vector is greater thanor b <- c(2, 4, 6)
equal to the correspondingelement of print(a>=b)
another vector. Output:

Dept. of CS 5
Statistical Computing & R Programming 2025

[1] FALSE TRUE FALSE


5 == This operator will return TRUE when every a <- c(1, 9, 5)
element inthe first vector is equal to the b <- c(1, 4, 6)
corresponding element of thesecond vector. print(a==b)
Output:
[1] TRUE FALSE FALSE
6 != This operator will return TRUE when every a <- c(1, 9, 5)
element inthe first vector is not equal tothe b <- c(1, 4, 6)
corresponding element ofthe second vector. print(a!=b)
Output:
[1] FALSE TRUE TRUE

Logical Operators: The logical operators allow a program to make a decision on the basis of multiple
conditions. In the program, each operand is considered as a condition which canbe evaluated to a false
or true value. The logical operator compares each elementof the first vector with the corresponding
element of the second vector.
[Link] Operator Description Example
1 & This operator is known as theLogical
a <- c(3, 0, TRUE, 2+2i)
AND operator. This operator takes the
b <- c(2, 4, TRUE, 2+3i)
first element of both the vector and returns
print(a&b)
TRUE if both the elements are TRUE.
Output:
[1] TRUE FALSE TRUE TRUE
2 | This operator is called the Logical OR
a <- c(3, 0, TRUE, 2+2i)
operator. This operator takes the first
element of both the vector and returns b <- c(2, 4, TRUE, 2+3i)
TRUE if one of them is TRUE. print(a|b)
Output:
[1] TRUE TRUE TRUE TRUE
3. ! This operator is known as Logical NOT
a <- c(3, 0, TRUE, 2+2i)
operator. This operator takes the first
element of the vector and gives the print(!a)
opposite logical value as a result. Output:
[1] FALSE TRUE FALSE
FALSE

4. && This operator takes the firstelement of


a <- c(3, 0, TRUE, 2+2i)
both the vector and gives TRUE as a
result, only if both are TRUE. b <- c(2, 4, TRUE, 2+3i)
print(a&&b)

5. || This operator takes the firstelement of


a <- c(3, 0, TRUE, 2+2i)
both the vector and gives the result TRUE,
if one of them is true. b <- c(2, 4, TRUE, 2+3i)
print(a||b)

Assignment Operators: 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.

Dept. of CS 6
Statistical Computing & R Programming 2025

[Link] Operator Description Example


1 <- or These operators areknown as left a <- c(3, 0, TRUE,
assignment operators. 2+2i)
= or b <<- c(2, 4,
<<- TRUE,2+3i)
d = c(1, 2,TRUE,2+3i)
print(a)
[1] 3+0i 0+0i 1+0i 2+2i
print (b)
[1] 2+0i 4+0i 1+0i 2+3i
print (d)
[1] 1+0i 2+0i 1+0i 2+3i
2. -> or ->> These operators areknown as right c(3, 0, TRUE, 2+2i) -> a
assignment operators. c(2, 4, TRUE, 2+3i) ->> b
print (a)
[1]3+0i 0+0i 1+0i 2+2i
print (b)
[1] 2+0i 4+0i 1+0i 2+3i

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

[Link] Operator Description Example


1 : The colon operator is used to create
v <- 1:8
the seriesof numbers in sequencefor a
print(v)
vector.
output:
[1] 1 2 3 4 5 6 7 8
2. %in% This is used when we want to identify
a1 <- 8
if anelement belongs to a vector.
a2 <- 12
d <- 1:10
print(a1%in%)
[1] FALSE
print(a2%in%t)
[1] FALSE
3. %*% It is used to multiply a matrix with its M = matrix( c(2,6,5,1,10,4),
transpose. nrow=2, ncol=3, byrow =TRUE)
t = M %*% t(M)
print(t)
[,1] [,2]
[1,] 65 82
[2,] 82 117
Data Structures in R Programming language:
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.

Dept. of CS 7
Statistical Computing & R Programming 2025

1. Vectors
A vector is an ordered collection of basic data types of a given length. All the elements of a vector must
be of the identical data type e.g homogeneous data structures. Vectors are one-dimensional data
structures.
Example:
X = c(1, 3, 5, 7, 8)
print(X)
Output:
[1] 1 3 5 7 8

2. Lists
A list is a generic object consisting of an ordered collection of objects. Lists are heterogeneous data
structures. These are also one-dimensional data structures. 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("ravi", "ram", "abhi", "kiran")
numberOfEmp = 4
empList = list(empId, empName, numberOfEmp)
print(empList)

Output:
[[1]]
[1] 1 2 3 4
[[2]]
[1] "ravi" "ram" "abhi" "kiran"
[[3]]
[1] 4

3. Data Frames
Data frames are generic data objects of R which are used to store the tabular data. They are two-
dimensional, heterogeneous data structures. These are lists of vectors of equal lengths. To create a data
frame we use the [Link]() function.
Example:
Name = c("Abhi", "Raj", "ram")
Language = c("R", "Python", "Java")
Age = c(22, 25, 45)
df = [Link](Name, Language, Age)
print(df)

Output:
Name Language Age
1 Abhi R 22
2 Raj python 25
Dept. of CS 8
Statistical Computing & R Programming 2025

3 Ram Java 45

4. Matrices
A matrix is a rectangular arrangement of numbers in rows and columns. Matrices are two-dimensional,
homogeneous data structures.
Example:
A = matrix (c (1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)
print(A)

Output:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9

5. Arrays
Arrays are the R data objects which store the data in more than two dimensions. Arrays are n-
dimensional data structures. For 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.
Example:
A = array(c(1, 2, 3, 4, 5, 6, 7, 8),dim = c(2, 2, 2))
print(A)
Output: [, , 1]
[,1] [,2]
[1,] 1 3
[2,] 2 4
[, , 2]
[,1] [,2]
[1,] 5 7
[2,] 6 8
6. Factors
Factors are the data objects which are used to categorize the data and store it as levels. They are
useful for storing categorical data. They can store both strings and integers. They are useful to
categorize unique values in columns like (“TRUE” or “FALSE”) or (“MALE” or “FEMALE”), etc.
They are useful in data analysis for statistical modelling.
Example:
# Creating factor using factor()
Dept. of CS 9
Statistical Computing & R Programming 2025

fac = factor(c("Male", "Female", "Male","Male", "Female", "Male", "Female"))


print(fac)
Output:
[1] Male Female Male Male Female Male Female
Levels: Female Male

R-Vectors
R Vectors are the same as the arrays in R language which are used to hold multiple data values of the
same type. In R Programming Language the indexing of the vector will start from '1' and not from '0'.
We can create numeric vectors and character vectors as well.

1. Creating a vector in R
A vector is a basic data structure that represents a one-dimensional array. To create a array we use
the "c" function which the most common method use in R Programming Language. We can also
use seq() function or use colons ":" also as shown in the example.
X<- c(61, 4, 21, 67, 89, 2)
cat('using c function', X, '\n')
Output:
using c function 61 4 21 67 89 2

Y<- seq(1, 10, [Link] = 5)


cat('using seq() function', Y, '\n')
Output:
using seq() function 1 3.25 5.5 7.75 10

Z<- 2:7
cat('using colon', Z)
Output:
using colon 2 3 4 5 6 7

2. Types of R vectors
Vectors are of different types which are used in R. Following are some of the types of vectors:

2.1 Numeric vectors


Numeric vectors are those which contain numeric values such as integer, float, etc. The L suffix in R
is used to specify that a number is an integer and not a numeric (floating-point) value.

v1 <- c(4, 5, 6, 7)
typeof(v1)
v2 <- c(1L, 4L, 2L, 5L)
typeof(v2)

Output:
[1] "double"
[1] "integer"

Dept. of CS 10
Statistical Computing & R Programming 2025

2.2 Character vectors


Character vectors in R contain alphanumeric values and special characters. In R, when a vector
contains elements of mixed types (like characters and numbers). Since characters are more general than
numbers in R, the vector is coerced to a character vector.
v1 <- c('hi', '2', 'hello', ‘57’)
typeof(v1)
Output:
[1] "character"

2.3 Logical vectors


Logical vectors in R contain Boolean values such as TRUE, FALSE and NA for Null values. In R, NA
is a special value used to represent missing or undefined data. When used in a logical vector, NA is
treated as a logical value because it is specifically designed to work with logical vectors and other types
of data.
v1 <- c(TRUE, FALSE, TRUE, NA)
typeof(v1)
Output:
[1] "logical"

3. Length of R vector
In R, the length of a vector is determined by the number of elements it contains. we can use the length()
function to retrieve the length of a vector.
x <- c(1, 2, 3, 4, 5)
length(x)
y <- c("apple", "banana", "cherry")
length(y)
z <- c(TRUE, FALSE, TRUE, TRUE)
length(z)

Output:
> length(x)
[1] 5
> length(y)
[1] 3
> length(z)
[1] 4

4. Accessing R vector elements


Accessing elements in a vector is the process of performing operation on an individual element of a
vector. There are many ways through which we can access the elements of the vector. The most
common is using the '[]', symbol.
Note: Vectors in R are 1 based indexing unlike the normal C, python, etc format.
X <- c(2, 5, 18, 1, 12)
cat('Using Subscript operator', X[2], '\n')
Output:
Using Subscript operator 5

Dept. of CS 11
Statistical Computing & R Programming 2025

Y <- c(4, 8, 2, 1, 17)


cat('Using combine() function', Y[c(4, 1)], '\n')
Output:
Using combine() function 1 4

5. Modifying a R vector
Modification of a Vector is the process of applying some operation on an individual element of a
vector to change its value in the vector. There are different ways through which we can modify a
vector:
X <- c(2, 7, 9, 7, 8, 2)
X[3] <- 1
X[2] <- 9
cat('subscript operator', X, '\n')
Output:
subscript operator 2 9 1 7 8 2

X[1:5] <- 0
cat('Logical indexing', X, '\n')
Output:
Logical indexing 0 0 0 0 0

X <- X[c(3, 2, 1)]


cat('combine() function', X)
Output:
combine() function 0 0 0

6. Deleting a R vector
Deletion of a Vector is the process of deleting all of the elements of the vector. This can be done by
assigning it to a NULL value.
M <- c(8, 10, 2, 5)
M <- NULL
print(cat('Output vector', M))
Output:
Output vector NULL

7. Sorting elements of a R Vector


sort() function is used with the help of which we can sort the values in ascending or descending
order.
X <- c(8, 2, 7, 1, 11, 2)
A <- sort(X)
cat('ascending order', A, '\n')
Output:
ascending order 1 2 2 7 8 11

B <- sort(X, decreasing = TRUE)


cat('descending order', B)
Output:
descending order 11 8 7 2 2 1

Dept. of CS 12
Statistical Computing & R Programming 2025

Vector Operation
1. Combining vectors: By combining one or more vectors, it forms a new vector which contains
all theelements of each vector.
Example: p<-c(1,2,4,5,7,8)
q<-c("shubham","arpita","nishka","gunjan","vaishali","sumit")
r<-c(p,q)
print (r)
Output: "1" "2" "4" "5" "7" "8"
"shubham" "arpita" "nishka" "gunjan" "vaishali" "sumit"

2. Arithmetic operations: We can perform all the arithmetic operation on vectors. The arithmetic
operationsare performed member-by-member on vectors.
Example:
a<-c(1,3,5,7)
b<-c(2,4,6,8)
print (a+b)
print (a-b)
output:3 7 11 15
-1 -1 -1 -1

R Lists
A list is a generic object consisting of an ordered collection of objects. Lists are one-
dimensional, heterogeneous data structures. The list can be a list of vectors, a list of matrices, a list
of characters, a list of functions, and so on. A list in R is created with the use of the list() function.
R allows accessing elements of an R list with the use of the index value. In R, the indexing of a list
starts with 1 instead of 0.

1. Creating a List
To create a List in R you need to use the function called "list()". We want to build a list of employees
with the details. So for this, we want attributes such as ID, employee name, and the number of
employees.
Example:
empId = c(1, 2, 3, 4)
empName = c("ravi", "sandeep", "ram", "vinay")
numberOfEmp = 4
empList = list(empId, empName, numberOfEmp)
print(empList)

Output
[[1]]
[1] 1 2 3 4
[[2]]
[1] "ravi" "sandeep" "ram" "vinay"
[[3]]
[1] 4

2. Naming List Components


Naming list components make it easier to access them.
Example:
my_named_list <- list(name = "Sudheer", age = 25, city = "Delhi")
print(my_named_list)
Dept. of CS 13
Statistical Computing & R Programming 2025

Output
$name
[1] "Sudheer"
$age
[1] 25
$city
[1] "Delhi"

3. Accessing R List Components


We can access components of an R list in two ways.
3.1. Access components by names:
All the components of a list can be named and we can use those names to access the
components of the R list using the dollar command.
Example:
empId = c(1, 2, 3, 4)
empName = c("ravi", "sandeep", "ram", "vinay")
numberOfEmp = 4
empList = list("ID" = empId,"Names" = empName,"Total Staff" = numberOfEmp)
print(empList)
cat("Accessing name components using $ command\n")
print(empList$Names)

Output
$ID
[1] 1 2 3 4
$Names
[1] "ravi" "sandeep" "ram" "vinay")
$`Total Staff`
[1] 4
Accessing name components using $ command
[1] ("ravi", "sandeep", "ram", "vinay")

3.2. Access components by indices:


We can also access the components of the R list using indices. To access the top-level components
of a R list we have to use a double slicing operator "[[ ]]".

Example:
empId = c(1, 2, 3, 4)
empName = c("ravi", "sandeep", "ram", "vinay")
numberOfEmp = 4
empList = list( "ID" = empId,"Names" = empName,"Total Staff" = numberOfEmp)
print(empList)
cat("Accessing name components using indices\n")
print(empList[[2]])
cat("Accessing Sandeep from name using indices\n")
print(empList[[2]][2])

cat("Accessing 4 from ID using indices\n")


print(empList[[1]][4])

Output
$ID

Dept. of CS 14
Statistical Computing & R Programming 2025

[1] 1 2 3 4
$Names
[1] ("ravi", "sandeep", "ram", "vinay")
$`Total Staff`
[1] 4

4. Modifying Components of a List


A R list can also be modified by accessing the components and replacing them with the
ones which you want.
Example:
empId = c(1, 2, 3, 4)
empName = c("ravi", "sandeep", "ram", "vinay")
numberOfEmp = 4
empList = list("ID" = empId,"Names" = empName,"Total Staff" = numberOfEmp)
cat("Before modifying the list\n")
print(empList)
empList$`Total Staff` = 5
empList[[1]][5] = 5
empList[[2]][5] = "kamala"
cat("After modified the list\n")
print(empList)

Output
Before modifying the list
$ID
[1] 1 2 3 4
$Names
[1] "ravi" "sandeep" "ram" "vinay")
$`Total Staff`
[1] 4
After modified the list
$ID
[1] 1 2 3 4 5
$Names
[1] "ravi" "sandeep" "ram" "vinay" “kamala”

5. Concatenation of lists
Two R lists can be concatenated using the concatenation function. So, when we want to concatenate
two lists we have to use the concatenation operator.
Syntax
list1 = cat(list, list)
list = the original list
list1 = the new list
Example:
empId = c(1, 2, 3, 4)
empName = c("ravi", "sandeep", "ram", "vinay")
empList = list("ID" = empId,"Names" = empName)
cat("Before concatenation of the new list\n")
print(empList)
empAge = c(34, 23, 18, 45)
empList = c(empName, empAge)
cat("After concatenation of the new list\n")
print(empList)

Dept. of CS 15
Statistical Computing & R Programming 2025

OUTPUT:
After concatenation of the new list
[1] "ravi" "sandeep" "ram" "vinay" "34" "23" "18" "45"

6. Adding Item to List


To add an item to the end of list, we can use append() function.
my_numbers = c(1,5,6,3)
append(my_numbers, 45)
my_numbers

Output
[1] 1 5 6 3 45

7. Deleting Components of a List


To delete components of a R list, and then insert a negative sign before those components.
Example:
empId = c(1, 2, 3, 4)
empName = c("ravi", "sandeep", "ram", "vinay")
empList = list("ID" = empId,"Names" = empName)
cat("Before deletion the list is\n")
print(empList)
$ID
[1] 1 2 3 4
$Names
[1] "ravi" "sandeep" "ram" "vinay"

cat("After Deleting name\n")


After Deleting name

print(empList[-2])
$ID
[1] 1 2 3 4

8. Merging list
We can merge the R list by placing all the lists into a single list.
lst1 <- list(1,2,3)
lst2 <- list("Sun","Mon","Tue")
new_list <- c(lst1, lst2)
print(new_list)

9. Converting List to Vector


To convert the R list to vector, first we create a list and then unlist the list into the vector.
lst <- list(1:5)
print(lst)
vec <- unlist(lst)
print(vec)

Output
[[1]]
[1] 1 2 3 4 5

[1] 1 2 3 4 5

Dept. of CS 16
Statistical Computing & R Programming 2025

10. R List to matrix


create matrices using matrix() function in R programming. Another function that will be used is
unlist() function to convert the lists into a vector.
lst1 <- list(list(1, 2, 3),
list(4, 5, 6))
cat("The list is:\n")
print(lst1)
cat("Class:", class(lst1), "\n")
mat <- matrix(unlist(lst1), nrow = 2, byrow = TRUE)
cat("\nAfter conversion to matrix:\n")
print(mat)
cat("Class:", class(mat), "\n")

R-Matrices
In R programming, matrices are two-dimensional, homogeneous data structures. In a matrix, rows
are the ones that run horizontally and columns are the ones that run vertically.

Creating a Matrix: To create a matrix, we use the function called matrix (). The arguments to
this matrix() are the set of elements in the vector.

Note: By default, matrices are in column-wise order.


Syntax: matrix(data, nrow, ncol, byrow, dimnames)

Parameters:
➢ data : values you want to enter
➢ nrow : no. of rows
➢ ncol : no. of columns
➢ byrow : logical value, if 'true' value will be assigned by rows
➢ dimnames : names of rows and columns
Example: A = matrix( c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)
rownames(A) = c("a", "b", "c")
colnames(A) = c("c", "d", "e")
cat("The 3x3 matrix:\n")
print(A)
Output
The 3x3 matrix:
cde
a123
b456
c789

2. Creating Special Matrices in R


R allows the creation of various different types of matrices with the use of arguments passed to the
matrix () function.

2.1 Matrix where all rows and columns are filled by a single constant 'k'
To create such a R matrix the syntax is given below:
Syntax
matrix(k, m, n)
Parameters:
• k: the constant

Dept. of CS 17
Statistical Computing & R Programming 2025

• m: no of rows
• n: no of columns
Example:
print(matrix(5, 3, 3))

Output
[,1] [,2] [,3]
[1,] 5 5 5
[2,] 5 5 5
[3,] 5 5 5

2.2 Diagonal matrix


A diagonal matrix is a matrix in which the entries outside the main diagonal are all zero.
Syntax
diag(k, m, n)
Parameters:
• k: the constants/array
• m: no of rows
• n: no of columns
Example:
print(diag(c(5, 3, 3), 3, 3))

Output
[,1] [,2] [,3]
[1,] 5 0 0
[2,] 0 3 0
[3,] 0 0 3

2.3 Identity matrix


An identity matrix in which all the elements of the principal diagonal are ones and all other elements
are zeros.
Syntax
diag(k, m, n)
Parameters:
• k: 1
• m: no of rows
• n: no of columns
Example:
print(diag(1, 3, 3))

Output
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1

2.4. Matrix Metrics


Matrix metrics tell you about the Matrix you created. You might want to know the number of rows,
number of columns, dimensions of a Matrix.

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

Dept. of CS 18
Statistical Computing & R Programming 2025

cat("The 3x3 matrix:\n")


print(A)
cat("Dimension of the matrix:\n")
print(dim(A))
cat("Number of rows:\n")
print(nrow(A))
cat("Number of columns:\n")
print(ncol(A))
cat("Number of elements:\n")
print(length(A))
print(prod(dim(A)))

Output
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Dimension of the matrix:
[1] 3 3
Number of rows:
[1] 3
Number of columns:
[1] 3
Number of elements:
Number of elements:
[1] 9
[1] 9

3. Accessing Elements of a Matrix


Value before the comma is used to access rows and value that is after the comma is used to access
columns.
3.1 Accessing rows: accessing specific rows in a matrix.
A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),nrow = 3,ncol = 3,byrow = TRUE)
cat("The 3x3 matrix:\n")
print(A)
cat("Accessing first and second row\n")
print(A[1:2, ])

Output
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Accessing first and second row
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6

3.2 Accessing columns: accessing specific columns in a matrix.


A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3,ncol = 3,byrow = TRUE)
cat("The 3x3 matrix:\n")

Dept. of CS 19
Statistical Computing & R Programming 2025

print(A)
cat("Accessing first and second column\n")
print(A[, 1:2])

Output
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Accessing first and second column
[,1] [,2]
[1,] 1 2
[2,] 4 5
[3,] 7 8

3.3 Accessing Elements of a matrix: accessing specific elements in a matrix.


A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)
cat("The 3x3 matrix:\n")
print(A)
print(A[1, 2]) # 1st row 2nd column
print(A[2, 3]) # 2nd row 3rd column

Output
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[1] 2
[1] 6

4. Accessing Submatrices in R: We can access the submatrix in a matrix using the colon (:) operator.
A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),nrow = 3,ncol = 3,byrow = TRUE)
cat("The 3x3 matrix:\n")
print(A)
cat("Accessing the first three rows and the first two columns\n")
print(A[1:3, 1:2])

Output
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
Accessing the first three rows and the first two columns
[,1] [,2]
[1,] 1 2
[2,] 4 5
[3,] 7 8

5. Modifying Elements of a Matrix: We can modify the elements of the matrices by a direct
assignment.

Dept. of CS 20
Statistical Computing & R Programming 2025

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


cat("The 3x3 matrix:\n")
print(A)
A[3, 3] = 30 # 3rd row 3rd column element is changed to 30
cat("After edited the matrix\n")
print(A)

Output
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
After edited the matrix
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 30

6. Matrix Concatenation: The merging of rows or columns of an existing R matrix.


6.1 Concatenation of a row: The concatenation of a row to a matrix is done using rbind().
A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),nrow = 3,ncol = 3,byrow = TRUE)
cat("The 3x3 matrix:\n")
print(A)
B = matrix(c(10, 11, 12),nrow = 1,ncol = 3)
cat("The 1x3 matrix:\n")
print(B)
C = rbind(A, B)
cat("After concatenation of a row:\n")
print(C)

Output
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
The 1x3 matrix:
[,1] [,2] [,3]
[1,] 10 11 12
After concatenation of a row:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
[4,] 10 11 12

6.2 Concatenation of a column: The concatenation of a column to a matrix is done using cbind().
A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9),nrow = 3, ncol = 3, byrow = TRUE)
cat("The 3x3 matrix:\n")
print(A)
B = matrix(c(10, 11, 12),nrow = 3,ncol = 1,byrow = TRUE)
cat("The 3x1 matrix:\n")

Dept. of CS 21
Statistical Computing & R Programming 2025

print(B)
C = cbind(A, B)
cat("After concatenation of a column:\n")
print(C)

Output
The 3x3 matrix:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
The 3x1 matrix:
[,1]
[1,] 10
[2,] 11
[3,] 12
After concatenation of a column:
[,1] [,2] [,3] [,4]
[1,] 1 2 3 10
[2,] 4 5 6 11
[3,] 7 8 9 12

7. Adding Rows and Columns in a Matrix


To add a row in matrix you can use rbind() function and to add a column to matrix you can use cbind()
function.

8. Deleting Rows and Columns of a Matrix


To delete a row or a column, first of all, access that row or column and then insert a negative sign
before that row or column. It indicates that you had to delete that row or column.
8.1 Row deletion: deleting a row in a matrix.
A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3,byrow = TRUE )
cat("Before deleting the 2nd row\n")
print(A)
A = A[-2, ]
cat("After deleted the 2nd row\n")
print(A)

Output
Before deleting the 2nd row
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
After deleted the 2nd row
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 7 8 9
8.2 Column deletion: deleting a column in a matrix.
A = matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3,byrow = TRUE)
cat("Before deleting the 2nd column\n")
print(A)
A = A[, -2]
cat("After deleted the 2nd column\n")

Dept. of CS 22
Statistical Computing & R Programming 2025

print(A)

Output
Before deleting the 2nd column
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
After deleted the 2nd column
[,1] [,2]
[1,] 1 3
[2,] 4 6
[3,] 7 9

Applications of matrix
➢ Matrix is the representation method which helps in plotting commonsurvey things.
➢ In robotics and automation, Matrices have the topmost elements for therobot movements.
➢ In computer-based application, matrices play a crucial role in the creationof realistic seeming
motion.
Arrays
Arrays are the data objects which allow us to store data in more than two dimensions. Array can be
created by using array() function. This function takes a vector as an input and to create an array it
usesvectors values in the dim parameter.
For example- if we create an array of dimension (2, 3, 4) then it will create4 rectangular matrices of
2 row and 3 columns.
Syntax:
array_name <- array(data, dim= (row_size, column_size, matrices, dim_names))

data: It is an input vector which is given to the array.


Matrices: The array consists of multi-dimensional matrices.
row_size: the number of row elements which an array can store.
column_size: the number of columns elements which an array can store.
dim_names: This parameter is used to change the default names of rows andcolumns.

Creation of an Array:
There are only two steps to create a matrix which are as follows
1. In the first step, we will create two vectors of different lengths.
2. Once our vectors are created, we take these vectors as inputs to the array.
Example:
#Creating two vectors of different lengths
vec1 <-c(1,3,5)
vec2 <-c(10,11,12,13,14,15)
#Taking these vectors as input to the array
res <- array(c(vec1,vec2),dim=c(3,3,2))
print(res)
Output:
,,1
[,1] [,2] [,3]
[1,] 1 10 13
Dept. of CS 23
Statistical Computing & R Programming 2025

[2,] 3 11 14
[3,] 5 12 15

,,2
[,1] [,2] [,3]
[1,] 1 10 13
[2,] 3 11 14
[3,] 5 12 15

Naming rows and columns


In R, we can give the names to the rows, columns, and matrices of the array. This is done with the
help of the dim name parameter of the array() function.
Example:
#Creating two vectors of different lengths
vec1 <-c(1,3,5)
vec2 <-c(10,11,12,13,14,15)
#Initializing names for rows, columns and matrices
col_names <- c("Col1","Col2","Col3")
row_names <-c("Row1","Row2","Row3")matrix_names <- c("Matrix1","Matrix2")
#Taking the vectors as input to the array
res <- array(c(vec1,vec2),dim=c(3,3,2),dimnames=list(row_names,col_names,m atrix_names))
print(res)

Output:
, , Matrix1
Col1 Col2 Col3

Row1 1 10 13
Row2 3 11 14
Row3 5 12 15

, , Matrix2
Col1 Col2 Col3
Row1 1 10 13
Row2 3 11 14
Row3 5 12 15

Accessing array elements


The elements are accessed with the help of the index.

Example: For the above created array


Print(res[3, ,2]) #To print third row of second matrix
Output: 5 12 15
Print(res[3,2,2]) #To print third row second column element of 2nd matrix
Output: 12
Print(res[ ,2,1]) #To print second column element of 1nd matrix
Output: 10 11 12
Dept. of CS 24
Statistical Computing & R Programming 2025

Manipulation of elements
The array is made up matrices in multiple dimensions so that the operations on elements of an
array are carried out by accessing elements of the matrices.
Example:
#Creating two vectors of different lengths
vec1 <-c(1,3,5)
vec2 <-c(10,11,12,13,14,15)
#Taking the vectors as input to the array1
res1 <- array(c(vec1,vec2),dim=c(3,3,1))
print(res1)
#Creating two vectors of different lengths
vec1 <-c(8,4,7)
vec2 <-c(16,73,48,46,36,73)
#Taking the vectors as input to the array2
res2 <- array(c(vec1,vec2),dim=c(3,3,1))
print(res2)
#Creating matrices from these arrays
res3 <-mat1+mat2
print(res3)
Output:
,,1
[,1] [,2] [,3]
[1,] 1 10 13
[2,] 3 11 14
[3,] 5 12 15

,,1
[,1] [,2] [,3]
[1,] 8 16 46
[2,] 4 73 36
[3,] 7 48 73

[,1] [,2] [,3]


[1,] 9 26 59
[2,] 7 84 50
[3,] 12 60 88

Data Frame
• A data frame is a two-dimensional array-like structure or a table in which a column contains
values of one variable, and rows contains one set of values fromeach column.
• A data frame is a special case of the list in which each component has equal length.
• A matrix can contain one type of data, but a data frame can contain different datatypes such as
numeric, character, factor, etc.
There are following characteristics of a data frame.
o The columns name should be non-empty.
o The rows name should be unique.
o The data which is stored in a data frame can be a factor, numeric, orcharacter type.
o Each column contains the same number of data items.

Dept. of CS 25
Statistical Computing & R Programming 2025

Creating Data Frame


The data frames are created with the help of frame() function of data. This function contains the vectors
of any type such as numeric, character, or integer.
Example: we create a data frame that contains employee id (integer vector), employee name(character
vector), salary(numeric vector), and starting date(Datevector).

# Creating the data frame.


[Link]<- [Link]( employee_id = c (1:5),
employee_name = c("ram","ravi","raj","anil","Sumit"),
sal = c(25000,35000,45000,50000,40000),
starting_date = [Link](c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11","2015-03-27")),
stringsAsFactors = FALSE)
print([Link]) # Printing the data frame.
Output:
employee_id employee_name sal starting_date
1 1 ram 25000 2012-01-01
2 2 ravi 35000 2013-09-23
3 3 raj 45000 2014-11-15
4 4 anil 50000 2014-05-11
5 5 Sumit 40000 2015-03-27

To print the structure of data frame


str([Link]) # Printing the structure of data frame.
Output:
'[Link]' : 5 obs. of 4 variables:
$ employee_id : int 1 2 3 4 5
$ employee_name: chr "ram" "ravi" "raj" "anil" “Sumit”
$ sal : num 25000 35000 45000 50000 40000
$ starting_date: Date, format: "2012-01-01" "2013-09-23" ...

Extracting data from Data Frame


We can extract the data in three ways which are as follows:
1. We can extract the specific columns from a data frame using the column name.
2. We can extract the specific rows also from a data frame.
3. We can extract the specific rows corresponding to specific columns.

Extracting the specific columns from a data frame


Example: For the above created data frame
# Extracting specific columns from a data frame
final <- [Link]([Link]$employee_id,[Link]$sal)
print(final)
Output:
[Link].employee_id [Link]
1 25000
Dept. of CS 26
Statistical Computing & R Programming 2025

2 35000
3 45000
4 50000
5 40000
Extracting the specific rows from a data frameExample
# Extracting first row from a data frame
final <-
[Link][1,]
print(final)
Output:
employee_id employee_name sal
starting_date
1 ram 25000 2012-01-01
Extracting specific rows corresponding to specific columnsExample:
# Extracting 2nd and 3rd row corresponding to the 1st and 4th column
final <-
[Link][c(2,3),c(1,4)]
print(final)
Output:
employee_id starting_date
2 201309-23
3 2014-11-15

Modification in Data Frame


• It is possible to add and delete rows and columns to the data frame.
Example: Adding rows and columns #Adding row in the data frame
x <- list(6,"Vaishali",547,"2015-09-
01")rbind([Link],x)
Output:
employee_id employee_name sal starting_date
1 1 ram 25000 2012-01-01
2 2 ravi 35000 2013-09-23
3 3 raj 45000 2014-11-15
4 4 anil 50000 2014-05-11
5 5 Sumit 40000 2015-03-27

#Adding column in the data frame


y <- c("Mysore","bangalore","mandya","mysore","kolar")
cbind([Link],Address=y)

Output:

employee_id employee_name sal starting_date Address


1 1 ram 25000 2012-01-01 Mysore
2 2 ravi 35000 2013-09-23 bangalore

Dept. of CS 27
Statistical Computing & R Programming 2025

3 3 raj 45000 2014-11-15 mandya


4 4 anil 50000 2014-05-11 mysore
5 5 Sumit 40000 2015-03-27 kolar

Logical-values
Introduction to Logical-values
• Logical-values can only take on two values: TRUE or FALSE.
• Logical-values represent binary states like
- >yes/no
->one/zero
• Logical-values are used to indicate whether a condition has been met or not.
TRUE and FALSE Notation
• Logical-values are represented as TRUE and FALSE.
• TRUE and FALSE are abbreviated as T and F, respectively.

Assigning Logical-values
Example:
b1 <- TRUE
b2 <- FALSE

Creating Vectors
• Vectors can be filled with logical-values using T or F.
Example:
myvec <- c(T,T,F,F,F)

Vector Length
• You can determine the length of a vector using the `length` function.
• Example:
length(myvec)
Output:5

A Logical Outcome: Relational Operators


Using Relational Operators
• Relational operators are used to find the relationship between two operands.
• The output of relational expression is either TRUE or FALSE.
• The 2 operands may be constants, variables or expressions.
• There are 6 relational operators:
Operator Meaning of Operator Example
> Greater than 4 > 5 returns FALSE
< Less than 4 < 5 returns TRUE
>= Greater than or equal to 4 >= 5 returns FALSE
<= Less than or equal to 4 <= 5 returns TRUE
== Equal to 4 == 5 returns FALSE
!= Not equal to 4 != 5 returns TRUE

any & all Functions


• any() checks whether at least one element in a vector meets a specific condition.

Dept. of CS 28
Statistical Computing & R Programming 2025

• It returns TRUE if any element satisfies the condition; otherwise, it returns


FALSE.
Example:
# Creating a vector
vector1 <- c(1, 2, 3, 4, 5)
# Checking if any element is greater than 3
result <- any(vector1 > 3)
Output:
TRUE
• all() checks whether all elements in a vector meet a specific condition.
• It returns TRUE if all elements satisfy the condition; otherwise, it returns
FALSE
Example:
# Creating a vector
vector2 <- c(1, 2, 3, 4, 5)
# Checking if all elements are greater than 0
result <- all(vector2 > 0)
Output:
TRUE

Multiple Comparisons: Logical Operators


Using Logical Operators for Multiple Conditions
• These operators are used to perform logical operations like negation,conjunction
and disjunction.
• The output of logical expression is either TRUE or FALSE.

Dept. of CS 29
Statistical Computing & R Programming 2025

Short and Long Versions


• There are versions of logical operators:
i) short versions
ii) long versions
i) Short versions are for element-wise [Link] versions return multiple logical-values.
Eg: `&`, `|`
• Element-wise comparisons are performed when comparing two vectors of equallength.
• Element-wise comparisons return a vector of logical-values.
• Example:
b1 <- c(T, F, F)
b2 <- c(F, T, F)
b1 & b2 returns [F, F, F].
b1 | b2 returns [T, T, F].

ii) Long versions are for comparing individual [Link] versions return a
single logical-value.
Eg: `&&`, `||`
• Using long versions of logical operators evaluates only the first pair of logical in
two vectors.
• Example:
`b1 <- c (T, F, F)
`b2 <- c (F, T, F)
`b1 && b2` returns `F`.
`b1 || b2` returns `T`.

Logical Subsetting and Extraction


• Logical sub setting and extraction involve using logical conditions to select elements
that satisfy a particular criterion.
• You create a logical vector with TRUE and FALSE values based on thecondition.
•Then, you use this vector to subset or extract elements.
Example:
# Creating a numeric vector
numeric_vector <- c(1, 2, 3, 4, 5)

# Logical subsetting to extract even numbers


even_numbers <- numeric_vector[numeric_vector %% 2 == 0]
Output:
24

Explanation of above program:


• We have a numeric vector called numeric_vector.
• We use the logical condition numeric_vector %% 2 == 0 to create a logicalvector
with TRUE for even numbers and FALSE for odd numbers.
• We use this logical vector for subsetting, resulting in even_numbers containingonly
the even elements from numeric_vector.

String
• A string is a data type.
• It is used to represent text or character data.
• Strings can consist of almost any combination of characters, including numbers.

Dept. of CS 30
Statistical Computing & R Programming 2025

• Strings are commonly used for storing and manipulating textual [Link]
e.g.: names, sentences, and text-data extracted from files or databases.

Creating a String
• You can create strings using single or double quotation marks.
Example:
single_quoted <- 'This is a single-quoted string.'
double_quoted <- "This is a double-quoted string."
Example: Str =” welcome to R Programming”
S=c(“java”,”C”,”C++”,”Python”)

String fruction:
1. Substr(String_name,Start,stop): Extract substring of given range from the string.
Print(substr(Str,1,7)
Output:Welcome
Print(substr(S,4,10)
Output:”a” --- ----“hon”

2. Grep(pattern,string): The grep() is a built-in function in R. It searches for matches for


certain character patterns within a character vector. The grep() takes pattern and data as an
argument and return a vector of indices of the character string.
Syntax:
grep(“pattern”, x)
Parameter:
• Pattern- The pattern that matches with given vector element
• x - specified character vector
Example: Program to show usage of grep()
# code
x <- c('Goods', 'GoodsforGoods', 'Good','Goodfor', 'Gfg')

# calling grep() function


grep('Good', x)
Output:
[1] 1 2 3 4

3. Sub(substring,replacement,input_string): Replace of substring will be done.


#create string
my_string <- 'This is a cool string'

#replace 'cool' with 'nice'


my_string <- sub('cool', 'nice', my_string)

#view updated string


my_string

Output:
[1] "This is a nice string"

4. substr() or substring(): function extracts substrings out of a string beginning with the
"start index" and ending with the "end index". It also replaces the specified substring with
a new set of characters.
substr("Learn Code Tech", 1, 4)
Output:

Dept. of CS 31
Statistical Computing & R Programming 2025

"Lear"

Extracts the first four characters from the string.


str <- c("program","with","a","new","language")
substr(str, 3, 3)<-c("%")
print(str)
Output:
"pr%gram" "wi%h" "ne%" "la%guage"

Replaces the third character of every string with % sign.


str <- c("program", "with", "new", "language")
substr(str, 3, 3) <- c("%", "@")
print(str)
Output:
"pr%gram" "wi@h" "ne%" "la@guage"

5. Paste(substring,stingsep=”delimeter”): Concatenation of string with given separator.


str <- paste("Java”, "C++")
print (str)
Output:
"Java c++"

6. Strsplit(string,split_string): It splits the string with reference given substring.


strsplit("good morning!", " ")
Output:
[1] "good" "morning" "!" “”

7. Tolower(string): convert given string into lowercase characters.


print (tolower(c("R PROGRAMMING ", "hI")))
Output :
"r programming" "hi"

8. Toupper(string): convert given string into uppercase characters.


print (toupper(c("r programming", "hI")))
Output :
"R PROGRAMMING" "HI"

9. Nchar(string): number of characters in a given character.


print (nchar(c("Learn", "Code")))
Output:
54

Finding String Length with nchar()


• nchar() can be used to determine the number of characters in a given string.
• It calculates and returns the length of a string in terms of the number of
characters
Example:
# Define a string
my_string <- "Hello, World!"

# Use nchar() to determine the length of the string


string_length <- nchar(my_string)

Dept. of CS 32
Statistical Computing & R Programming 2025

# Print the result


cat("The length of the string is:", string_length)
Output:
The length of the string is: 13
Explanation of above program:
• We define a string my_string containing the text "Hello, World!".
• We use the nchar() function to calculate the length of the string, including spacesand
special characters.
• The result is stored in the string_length variable.
• We then use cat() to display the length of the string.

Concatenation
Concatenation Functions
• Two main functions are used for concatenating strings: `cat` and `paste`.

1) Using the cat() Function


• cat() can be used for concatenating and printing strings with optional separators.
• Example:
# Concatenate and print two strings with a space separator
cat("Hello", "World")
Output:
"Hello World"
2) Using the paste() Function
• paste() can be used to concatenate multiple strings into one, with optional
separator and other arguments.
Example:
# Concatenate two strings with a space separator
concatenated <- paste("Hello", "World")
Output:
"Hello World"
cat() vs. paste()
• cat() is used for printing (displaying) text to the console. It concatenates and prints its
arguments with optional separators.
• paste() is used for concatenating strings into a single character [Link] concatenated string
can be assigned to a variable for further use.
It doesn't print directly to the console.

Separator (sep) Argument


• An optional argument `sep` is used as a separator between concatenated strings.
• Example:
# Concatenate two strings with a custom separator
concatenated <- paste("Hello", "World", sep = ", ")
#Output: "Hello, World"

Escape Sequences
Backslash (\) Usage
Dept. of CS 33
Statistical Computing & R Programming 2025

• The backslash (\) is used to invoke an escape sequence.


• Escape sequences allow you to enter characters that control the format and
spacing of the string.

Common Escape Sequences


`\n` starts a newline.
`\t` represents a horizontal tab.
`\b` invokes a backspace.
`\\` is used to include a single backslash.
`\"` includes a double quote.

Substrings and Matching


Pattern Matching
• Pattern matching allows you to inspect a given string to identify smaller stringswithin it.
• substr() can be used to extract substrings within a given string
Example:
# Extracting a substring from a string
original_string <- "Hello, World!"
substring <- substr(original_string, start = 1, stop = 5)
Output:
"Hello"
sub() can be used for replacing the first occurrence of a pattern within a string
Example:
# Replacing the first occurrence of "apple" with "banana"
text <- "I like apples, but apples are red."
new_text <- sub("apple", "banana", text)
Output:
I like bananas, but apples are red."

gsub() can be used for replacing all occurrences of a pattern within a string.
Example:
# Replacing all occurrences of "apple" with "banana"
text <- "I like apples, but apples are red."
new_text <- gsub("apple", "banana", text)
Output:
I like bananas, but bananas are red."
Special Values
When a data set has missing observations or when a practically infinite number is calculated the
software has some unique terms reserved for these situations. They are;

1) NA (Not Available): - If the value is not defined, data value is out of range,in such cases
NA values be printed as output.
Example: X<-c (1,2,3)
X [4]
Output: NA
2) INF and -INF: When a number is too large for R to represent, the value isgiven as Infinite.
Example:
r> 1/0
Output: INF
3) Nan (Not a Number): In some situations, it is impossible to express the result of
calculation using number, in such cases Nan is given as the output.
Dept. of CS 34
Statistical Computing & R Programming 2025

Example: factorial (-66)


4) NULL: This value is used to explicitly define an empty entity.
Example: f < - NULL
Print(f)
Output: NULL
Coercion
In R programming, converting from one object or data type to another object ordata type is
referred as coercion.
Ther are two types of coercion. They are:
Implicit coercion: This type of coercion occurs automatically.
Example: The logical value True will be treated as 1 and False will be treated as 0.

Explicit coercion: This type of coercion can be done with the help of [Link], [Link] etc.,
functons.
Example:
x <- c(0,1,0,3)
Class (x)
Output: “numeric”
[Link](x)
Class(x)
Output: “integer”
[Link](x)
Class(x)
Output: “complex”

Classes in R Programming
A class is just a blueprint or a sketch of these objects. It represents the set of properties or methods
that are common to all objects of one type.
Unlike most other programming languages, R has a three-class system. These are
➢ S3
➢ S4
➢ Reference Classes.

Classes and Objects in R


A class is a user-defined data type from which objects are created. They are also referred to as blue
prints sometimes. This is because they define the structure of objects. A class is a passive entity
whereas an object is an active entity.

R considers all things as objects. An object is a single instance of a class. R provides us a class()
function which can be either used to define the class of a function or get the information of the class
for an already defined object.
A class in R is present in a vector form. Due to this property, the objects can inherit from many
classes, and also we can specify the order of inheritance even for complex classes.
There are 3 types of Class:
S3 Class
S4 Class
Reference Class

Dept. of CS 35
Statistical Computing & R Programming 2025

S3 Class
S3 is the simplest yet the most popular OOP system and it lacks formal definition and structure. An
object of this type can be created by just adding an attribute to it. Following is an example to make
things more clear:
Example:
# create a list with required components
movieList <- list(name = "Iron man", leadActor = "Robert Downey Jr")

# give a name to your class


class(movieList) <- "movie"
movieList

Output
$name
[1] "Iron man"
$leadActor
[1] "Robert Downey Jr

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)

Output:
$name
[1] "Iron man"
$leadActor
[1] "Robert Downey Jr"

Example: Creating a user-defined print function

# now let us write our method


[Link] <- function(obj)
{
cat("The name of the movie is", obj$name,".\n")
cat(obj$leadActor, "is the lead actor.\n")
}

Output:
The name of the movie is Iron man .
Robert Downey Jr is the lead actor.

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.

Dept. of CS 36
Statistical Computing & R Programming 2025

Example:
library(methods)

# definition of S4 class
setClass("movies", slots=list(name="character", leadActor = "character"))

# creating an object using new() by passing class name and slot values
movieList <- new("movies", name="Iron man", leadActor = "Robert Downey Jr")

movieList

Output:
An object of class "movies"
Slot "name":
[1] "Iron man"
Slot "leadActor":
[1] "Robert Downey Jr"

As shown in the above example, setClass() is used to define a class and new() is used to create the
objects.

3. R6 Classes
R6 is an object-oriented system in R that provides more traditional object-oriented features such as
inheritance and encapsulation. R6 is more similar to classes in languages like Python or C++.

Creating an R6 Class
# Install and load R6 package if not installed
[Link]("R6")
library(R6)

# Define an R6 class
Person <- R6Class("Person",
public = list(
name = NULL,
age = NULL,

initialize = function(name, age) {


self$name <- name
self$age <- age
},

print_details = function() {
cat("Person's Name:", self$name, "\n")
cat("Person's Age:", self$age, "\n")
}
)
)

Dept. of CS 37
Statistical Computing & R Programming 2025

# Create an instance of the 'Person' class


john_r6 <- Person$new("John", 30)

# Call the method


john_r6$print_details()

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


movies <- setRefClass("movies", fields = list(name = "character",
leadActor = "character", rating = "numeric"))

#now we can use the generator to create objects


movieList <- movies(name = "Iron Man",
leadActor = "Robert downey Jr", rating = 7)
movieList

Output:
Reference class object of class "movies"
Field "name":
[1] "Iron Man"
Field "leadActor":
[1] "Robert downey Jr"
Field "rating":
[1] 7

Basic plotting in R:
The plot() function is used to draw points (markers) in a diagram. The function takes parameters
for specifying points in the diagram.
Parameter 1 specifies points on the x-axis.
Parameter 2 specifies points on the y-axis.
Example
Draw two points in the diagram, one at position (1, 3) and one in position (8, 10):
1) plot(c(1, 8), c(3, 10))

Dept. of CS 38
Statistical Computing & R Programming 2025

2) x<-c(1, 2, 3, 4, 5)
y<-c(3, 7, 8, 9, 12)
plot(x, y)

Draw a Line
The plot() function also takes a type parameter with the value l to draw a line to connect all the
points in the diagram:
plot(1:10, type="l")

Plot Labels
The plot() function also accept other parameters, such as main, xlab and ylab if you want to
customize the graph with a main title and different labels for the x and y-axis:
Example: plot(1:10, main="My Graph", xlab="The x-axis", ylab="The y axis")

Dept. of CS 39
Statistical Computing & R Programming 2025

Colors
Use col="color" to add a color to the points:
Example:
plot(1:10, col="red")

Size
Use cex=number to change the size of the points
Example: plot(1:10, cex=2)

Point Shape
Use pch with a value from 0 to 25 to change the point shape format:Example:
plot(1:10, pch=25, cex=2)

To change the colour of line


Example: plot(1:10, type=”l”, colour=”blue”)

Dept. of CS 40
Statistical Computing & R Programming 2025

To change the width of the line:


Example: plot(1:10, type=”l”,

QUESTION:
1. Explain the features of R Programming.
2. What is a vector? Give an example.
3. What is Coercion? Explain its types with an example.
4. Explain any two basic plotting in R with examples.
5. Write a note on special values in R?
6. Explain the advantage and disadvantage of R programming.
7. Explain datatypes with example.
8. Explain operators with example.
9. Explain the manipulation of list elements with example.
10. Explain the array function with example.
11. What is matrix? explain the matrix () function with example.
12. Define data frame? Explain the [Link]() function with example.
13. What is class? Explain the different types of classes.
14. What is string? Explain the different built-in function of statements

Dept. of CS 41

You might also like