Introduction To R in Data Analytics
Introduction To R in Data Analytics
Module-2
Introduction to R
“VIT AP”->name
x <- 9i + 3
class(x) #complex
• Relational Operators:
• Relational operators are used to compare two values.
• Returns a boolean TRUE value if the first operand satisfies the relation
compared to the second and Returns FALSE otherwise.
• A TRUE value is always considered to be greater than the FALSE.
• R supports these relational operators <,>,<=,>=,==,!=
24-01-2025 Dr. V. Srilakshmi
• Logical Operators:
• Logical operators are used to combine conditional statements.
• Logical operations in R simulate element-wise decision operations, based on
the specified operator between the operands, which are then evaluated to
either a True or False boolean value.
• Any non-zero integer value is considered as a TRUE value, be it a complex or
real number.
• Example:
var=as.integer(readline("enter some data"));
print(var);
24-01-2025 Dr. V. Srilakshmi
EXAMPLE PROGRAM:
# R PROGRAM TO FIND AREA AND PERIMETER OF A RECTANGLE.
l=as.integer(readline("enter length"))
b=as.integer(readline("enter breadth"))
Area=l*b
Pm=2*(l+b)
cat("Area=",Area)
cat("\nPertimerter=",Pm)
Syntax: if(expression){
statements
....
....
}
5. Switch Statement:
• In this switch function expression is matched to list of cases.
• If a match is found, then it prints that case’s value.
• No default case is available here.
• If no case is matched it outputs NULL.
Syntax: switch (expression, case1, case2, case3,…,case n )
Output:
University
• x = vector("numeric", 5)
• print("Numeric Type:")
• print(x)
• c = vector("complex", 5)
• print("Complex Type:")
• print(c)
• print("Logical Type:")
• print(l)
• chr = vector("character", 5)
• print("Character Type:")
• print(chr)
z=x+y
print(z)
Output:
[1] 30 30 70
24-01-2025 Dr. V. Srilakshmi
Write a R program to append value to a given vector.
x = c(10, 20, 30)
x=c(x,34)
print(x)
Output:
[1] 10 20 30 34
• print("Original Vectors:")
• print(x)
• print(sum(x==20))
• Output: 3
• intersect(x,y)
• print(result)
for(x in 1:5)
{
print(x)
}
Output:
[1] 1
[1] 2
[1] 3
[1] 4
[1] 5
24-01-2025 Dr. V. Srilakshmi
Example: Output:
[1] "Winter, month 1"
[1] "Winter, month 2"
for (month in 1:5) [1] "Spring, month 3"
{ [1] "Spring, month 4“
if (month < 3) [1] "Spring, month 5"
{
print(paste('Winter, month', month))
}
else {
print(paste('Spring, month', month))
}
}
24-01-2025 Dr. V. Srilakshmi
Lists
• A list in R 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 and a list
of functions, and so on.
• To create a List in R you need to use the function called “list()”.
• Example:
• # List of strings
thislist <- list("apple", "banana", "cherry")
# Print the list
thislist
• You can access the list items by referring to its index number, inside brackets.
The first item has index 1, the second item has index 2 and so on.
• Example:
• thislist <- list("apple", "banana", "cherry")
thislist[1] #Output: [[1]]
24-01-2025 Dr. V. Srilakshmi
[1] apple
Lists
• To change the value of a specific item, refer to the index number.
• Example:
• thislist <- list("apple", "banana", "cherry")
thislist[1] <- "blackcurrant"
# Print the updated list
thislist
You can specify a range of indexes by specifying where to start and where to end the
range, by using the : operator
Example:
• thislist=list("apple", "banana", "cherry", "orange", "kiwi", "melon")
matrixD = matrixC[-c(1),-c(1)]
matrixD
24-01-2025 Dr. V. Srilakshmi
Matrices
• Use the dim() function to find the number of rows and columns in a Matrix.
• Example:
matrixA=matrix(c(1,2,3,4,5,6,7,8,9),nrow=3,ncol=3)
matrixA
dim(matrixA)
cat("number of rows = ",nrow(matrixA))
cat("\nnumber of rows = ",ncol(matrixA))
newmatrixA=rbind(matrixA,matrixB)
newmatrixA
newmatrixA=cbind(matrixA,matrixB)
newmatrixA
In the above example, The first and second number in the bracket specifies the amount
of rows and columns.
The last number in the bracket specifies how many dimensions we want
24-01-2025 Dr. V. Srilakshmi
Arrays
• You can access the array elements by referring to the index position. You can
use the [] brackets to access the desired elements from an array.
• Syntax: array[row position, column position, matrix level]
• Example:
# An array with one dimension with values ranging from 1 to 24
thisarray <- c(1:24)
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[2, 3, 2]
In the above example, we are trying to access Dim→1
the element of multiarray located in
Row-2
Col-3
Dimention-2
Dim→2
24-01-2025 Dr. V. Srilakshmi
Arrays
• You can also access the whole row or column from a matrix in an array, by
using the c() function.
• Syntax: array[row position, column position, matrix level]
• Example:
thisarray <- c(1:24)
# Access all the items from the first row from matrix one
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[c(1),,1] #output: 1 5 9
# Access all the items from the first column from matrix one
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray[,c(1),2] #output: 13 14 15 16
24-01-2025 Dr. V. Srilakshmi
Data Frames
• Data Frames are data displayed in a format as a table.
• Data Frames can have different types of data inside it. While the first column
can be character, the second and third can be numeric or logical. However,
each column should have the same type of data.
• Example
• # Create a data frame
Data_Frame <- data.frame (
Training = c("Strength", "Stamina", "Other"),
Pulse = c(100, 150, 120),
Duration = c(60, 30, 45)
)
# Print the data frame
Data_Frame
summary(Student)
24-01-2025 Dr. V. Srilakshmi
Data Frames
• We can use single brackets [ ], double brackets [[ ]] or $ to access columns
from a data frame
• Example:
# Accessing a data frame
Student <- data.frame (
Name = c("John", "Anne", "Joyce","Kavitha"),
Age = c(23, 25, 22,26),
Rank = c(2, 3, 1,4)
)
To change the ordering of the rows in the data frame we use order()
function
f[order(f$age), ] #all elements in the data frame are printed ordered
by age
f[order(f$age), decreasing=TRUE ]
24-01-2025 Dr. V. Srilakshmi
Examples:
#R Program to demonstrate the use of function
>subtract_two_nums <- function(x, y)
{
return (x – y)
}
> print(subtract_two_nums(3, 1))
• Output:
[1] 2