R Programming Basics and Features
R Programming Basics and Features
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.
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 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.
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
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)
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()
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)
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.
Dept. of CS 4
Statistical Computing & R Programming 2025
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
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
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
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
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
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
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:
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
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
Dept. of CS 11
Statistical Computing & R Programming 2025
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
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
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
Output
$name
[1] "Sudheer"
$age
[1] 25
$city
[1] "Delhi"
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")
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])
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
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"
Output
[1] 1 5 6 3 45
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)
Output
[[1]]
[1] 1 2 3 4 5
[1] 1 2 3 4 5
Dept. of CS 16
Statistical Computing & R Programming 2025
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.
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.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
Output
[,1] [,2] [,3]
[1,] 5 0 0
[2,] 0 3 0
[3,] 0 0 3
Output
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
Dept. of CS 18
Statistical Computing & R Programming 2025
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
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
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
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
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
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
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))
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
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
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
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
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
Output:
Dept. of CS 27
Statistical Computing & R Programming 2025
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
Dept. of CS 28
Statistical Computing & R Programming 2025
Dept. of CS 29
Statistical Computing & R Programming 2025
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`.
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”
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"
Dept. of CS 32
Statistical Computing & R Programming 2025
Concatenation
Concatenation Functions
• Two main functions are used for concatenating strings: `cat` and `paste`.
Escape Sequences
Backslash (\) Usage
Dept. of CS 33
Statistical Computing & R Programming 2025
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
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.
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")
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"
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,
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
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)
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)
Dept. of CS 40
Statistical Computing & R Programming 2025
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