M.tech - Data Science Lab
M.tech - Data Science Lab
LAB MANUAL
for
DATA SCIENCE LAB
I
R19 M.TECH. CSE/CS
Course Objectives:
To make students understand learn about a Big Data – R Programming, way of solving
problems.
To teach students to write programs in Scala to solve problems.
Introduction to R Programming:
What is R and RStudio? R is a statistical software program. It has extremely useful tools for data
exploration, data analysis, and data visualization. It is flexible and also allows for advanced
programming. RStudio is a user interface for R, which provides a nice environment for working with R.
1. Write an R program to evaluate the following expression ax+b/ax-b.
2. Write an R program to read input from keyboard (hint: readLine()).
3. Write an R program to find the sum of n natural numbers: 1+2+3+4+….n
4. Write an R program to read n numbers.
(i) Sum of all even numbers (ii) Total number of even numbers.
5. Write an R program to read n numbers.
(i) Total number of odd numbers (ii) Sum of all odd numbers
6. Write an R program to obtain
(i)sum of two matrices A and B (ii) subtraction of two matrices A and B
(iii) Product of two matrices.
7. Write an R program for “declaring and defining functions “
8. Write an R program that uses functions to add n numbers reading from keyboard
9. Write an R program uses functions to swap two integers.
10. Write an R program that use both recursive and non-recursive functions for implementing the
Factorial of a given number, n.
11. Write an R program to reverse the digits of the given number {example 1234 to be written as
4321}
12. Write an R program to implement
(i)Linear search (ii) Binary Search.
13. Write an R program to implement
(i)Bubble sort (ii) selection sort.
14. Write a R program to implement the data structures
(i) Vectors (ii) Array (iii) Matrix (iv) Data Frame (v) Factors
15. Write a R program to implement scan(), merge(), read.csv() and read.table() commands.
16. Write an R program to implement “Executing Scripts” written on the note pad, by
calling to the R console.
17. Write a R program, Reading data from files and working with datasets
(i) Reading data from csv files, inspection of data.
(ii) Reading data from Excel files.
18. Write a R program to implement Graphs
(i) Basic high-level plots (ii)Modifications of scatter plots
(iii) Modifications of histograms, parallel box plots.
II
R19 M.TECH. CSE/CS
Suggested Links:
1. https://www.tutorialspoint.com/scala/
2. https://www.tutorialspoint.com/r/
II
DATA SCIENCE LAB (Lab-IV)
M.Tech-CSE/CS-I Year-II Semester
List of Experiments:
R program Code:
a <- 10
x <- 25
b <- 6
s=(a*x+b)/(a*x-b);
print(s)
Output:
1.04918
1
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
2. Write an R program to read input from keyboard (hint: readLine()).
R program Code:
# R program to illustrate to take input from the user through keyboard
# taking input using readline(), this command will prompt us to input a desired value
var = readline();
Output:
255
[1] 255
(Or)
Output:
Input your name: hari
Input your age: 12
[1] "My name is hari and I am 12 years old."
[1] "R version 4.1.1 (2021-08-16)"
2
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
The above code will be executed in the R/RStudio Console as given below:
> name = readline(prompt="Input your name: ")
Input your name: hello
> age = readline(prompt="Input your age: ")
3
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
3. Write an R program to find the sum of n natural numbers: 1+2+3+4+…n
R program Code:
# take input from the user
num = as.integer(readline(prompt = "Enter a number: "))
if(num < 0) {
print("Enter a positive number")
} else {
sum = 0
# use while loop to iterate until zero
while(num > 0) {
sum = sum + num
num = num - 1
}
print(paste("The sum is", sum))
}
Output:
Enter a number: 78
[1] "The sum is 3081"
The above code will be executed in the R/RStudio Console as given below:
> # take input from the user
> num = as.integer(readline(prompt = "Enter a number: "))
Enter a number: 78
> if(num < 0) {
+ print("Enter a positive number")
+ } else {
+ sum = 0
+ # use while loop to iterate until zero
+ while(num > 0) {
+ sum = sum + num
+ num = num - 1
+}
+ print(paste("The sum is", sum))
+}
[1] "The sum is 3081"
>
4
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
4. Write an R program to read n numbers
(i) Sum of all even numbers (ii) Total number of even numbers.
R program Code:
numbers <- 1:32
N <- length(numbers)
total <- rep(0,N)
for (i in numbers){
if(i %% 2 == 0) total[i] <-i
}
sum(total)
Output:
[1] 272
5
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
Sum of all even digits
R Program Code:
n = as.integer(readline(prompt = "Enter a number :"))
s=0
while (n > 0) {
r = n %% 10
if (r %% 2 == 0) {
s=s+r
}
n = n %/% 10
}
print(paste("Sum of all even digits :", s))
Output:
Enter a number : 1238
[1] "Sum of all even digits : 10"
The above code will be executed in the R/RStudio Console as given below:
> n = as.integer(readline(prompt = "Enter a number :"))
Enter a number : 1238
> s=0
> while (n > 0) {
+ r = n %% 10
+ if (r %% 2 == 0) {
+ s=s+r
+ }
+ n = n %/% 10
+ }
> print(paste("Sum of all even digits :", s))
[1] "Sum of all even digits : 10"
>
6
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
(ii) Total number of even numbers
R program Code:
x <- c(2,5,3,9,8,11,6)
count <- 0
for (val in x) {
if(val %% 2 == 0) count = count+1
}
print(count)
Output:
[1] 3
7
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
5. Write an R program to read n numbers
(i) Total number of odd numbers (ii) Sum of all odd numbers
R program Code:
x <- c(2,5,3,9,8,11,6)
count <- 0
for (val in x) {
if(val %% 2!= 0) count = count+1
}
print(count)
Output:
[1] 4
(Or)
R Program Code:
# Counts the number of odd integers in x (the argument vector)
#First define the function countoddnum
countoddnum <- function(x) {
#k (a counter) is used to count the number of odd numbers in x.
k <- 0
#In each iteration, n takes the value of the corresponding element x
#In test case one below , the loop iterates 7 times because the vector x has 7 elements.
for (n in x) {
if (n %% 2 == 1) k <- k+1
}
return(k)
}
#Then call the function on a couple of test cases
#Test case one
#It can be seen that x contains 4 odd numbers for test case one
countoddnum(c(1,3,5,23,22,44,66))
Output:
[1] 4
8
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
(ii) Sum of all odd numbers
R program Code:
numbers <- 1:32
N <- length(numbers)
total <- rep(0,N)
for (i in numbers){
if(i %% 2!= 0) total[i] <-i
}
sum(total)
Output:
[1] 256
9
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
Sum of all odd digits
R Program Code:
n = as.integer(readline(prompt = "Enter a number :"))
s=0
while (n > 0) {
r = n %% 10
if (r %% 2 != 0) {
s=s+r
}
n = n %/% 10
}
print(paste("Sum of all odd digits :", s))
Output:
Enter a number : 1834
[1] "Sum of all odd digits : 4"
The above code will be executed in the R/RStudio Console as given below:
> n = as.integer(readline(prompt = "Enter a number :"))
Enter a number : 1834
> s=0
> while (n > 0) {
+ r = n %% 10
+ if (r %% 2 != 0) {
+ s=s+r
+ }
+ n = n %/% 10
+ }
> print(paste("Sum of all odd digits :", s))
[1] "Sum of all odd digits : 4"
>
10
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
6. Write an R program to obtain
(i) sum of two matrices A and B (ii) subtraction of two matrices A and B
(iii) Product of two matrices
print(B)
result = A + B
print("Result of addition")
print(result)
Output:
[1] "Matrix-1:"
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[1] "Matrix-2:"
[2,] 3 7 8
11
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
(ii) subtraction of two matrices A and B
R program Code:
# Create two 2x3 matrixes.
A = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
print("Matrix-1:")
print(A)
B = matrix(c(0, 1, 2, 3, 0, 2), nrow = 2)
print("Matrix-2:")
print(B)
result = A - B
print("Result of subtraction")
print(result)
Output:
[1] "Matrix-1:"
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[1] "Matrix-2:"
[2,] 1 1 4
12
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
(iii) Product of two matrices
R program Code:
# Create two 2x3 matrixes.
A = matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
print("Matrix-1:")
print(A)
B = matrix(c(0, 1, 2, 3, 0, 2), nrow = 2)
print("Matrix-2:")
print(B)
result = A * B
print("Result of multiplication")
print(result)
Output:
[1] "Matrix-1:"
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
[1] "Matrix-2:"
[,1] [,2] [,3]
[1,] 0 2 0
[2,] 1 3 2
[1] "Result of multiplication"
[,1] [,2] [,3]
[1,] 0 6 0
[2,] 2 12 12
13
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
7. Write an R program for “declaring and defining functions”
Syntax:
f = function(arguments){
statements
Here f = function name
R program Code:
# A simple R function to check whether x is even or odd
evenOdd = function(x){
if(x %% 2 == 0)
return("even")
else
return("odd")
}
print(evenOdd(4))
print(evenOdd(3))
Output:
[1] "even"
[1] "odd"
14
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
Multiple Inputs and Multiple Outputs with Functions Concept
R program Code:
# A simple R function to calculate area and perimeter of a rectangle
Rectangle = function(length, width){
area = length * width
perimeter = 2 * (length + width)
# create an object called result which is a list of area and perimeter
result = list("Area" = area, "Perimeter" = perimeter)
return(result)
}
resultList = Rectangle(2, 3)
print(resultList["Area"])
print(resultList["Perimeter"])
Output:
$Area
[1] 6
$Perimeter
[1] 10
15
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
8. Write an R program that uses functions to add n numbers reading from keyboard
R program Code:
num1 = as.integer(readline(prompt="Enter a number 1: "))
Output:
Enter a number 1: 87
Enter a number 2: 67
Enter a number 3: 54
Enter a number 4: 45
[1] 253
16
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
9. Write an R program that uses functions to swap two integers.
Output:
Enter x value :89
17
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
ii) R Program to swap two integers by using third variable
R Program Code:
x <- as.integer(readline(prompt = "Enter x value :"))
y <- as.integer(readline(prompt = "Enter y value :"))
print(paste("Before swap x is :", x))
print(paste("Before swap y is :", y))
temp = x
x=y
y = temp
print(paste("After swap x is :", x))
print(paste("After swap y is :", y))
Output:
Enter x value :87
18
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
10. Write an R program that use both recursive and non-recursive functions for
implementing Factorial of a given number, n.
R Program Code:
recur_factorial <- function(n) {
if(n <= 1) {
return(1)
} else {
return(n * recur_factorial(n-1))
}
}
recur_factorial(5)
(Or)
rec_fac <- function(x){
if(x==0 || x==1)
{
return(1)
}
else
{
return(x*rec_fac(x-1))
}
}
rec_fac(5)
Output:
[1] 120
19
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
ii) R Program to Find the Factorial of a Number Using Non-Recursive Function (Non-
Recursion)
R Program Code:
# take input from the user
num = as.integer(readline(prompt="Enter a number: "))
factorial = 1
for(i in 1:num) {
factorial = factorial * i
}
print(paste("The factorial of", num ,"is",factorial))
}
Output:
Enter a number: 7
[1] "The factorial of 7 is 5040"
20
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
11. Write an R program to reverse the digits of the given number {example 1234 to be
written as 4321}
R program Code:
n = as.integer(readline(prompt = "Enter a number :"))
rev = 0
while (n > 0) {
r = n %% 10
rev = rev * 10 + r
n = n %/% 10
}
print(paste("Reverse number is :", rev))
Output:
Enter a number :1234
[1] "Reverse number is : 4321"
21
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
12. Write an R program to implement
(i) Linear search (ii) Binary search.
(i) Linear search
R Program Code:
arr= c(5,8,4,6,9,2)
x=9
if(any(arr == x)) 'Found' else 'Not Found'
Output:
[1] "Found"
Without if/else :
c('Not found', 'Found')[any(arr == x) + 1]
22
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
(ii) Binary search
R Program Code:
BiSearch <- function(table, key) {
# Takes sorted (in ascending order) vectors
stopifnot(is.vector(table), is.numeric(table))
r <- length(table)
m <- ceiling(r / 2L) # Midpoint
if (table[m] > key) {
if (r == 1L) {
return(FALSE)
}
BiSearch(table[1L:(m - 1L)], key)
BiSearch(seq(from = 0, to = 1, by = 0.1), 0.3)
}
else {
return(TRUE)
}
}
Output:
[1] FALSE
23
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
13. Write an R program to implement
(i) Bubble sort (ii) selection sort.
(i) Bubble sort
R Program Code:
# function to sort the array using bubble sort
bubble_sort <- function(x)
{
# calculate the length of array
n <- length(x)
# run loop n-1 times
for (i in 1 : (n - 1)) {
# run loop (n-i) times
for (j in 1 : (n - i)) {
# compare elements
}
x
}
24
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
# sort the array and store the result in sorted_array
sorted_array <- bubble_sort(arr)
# print sorted_array
sorted_array
Output:
[1] 12 16 20 26 29 50 79 81 83 90
25
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
(ii) selection sort
R Program Code:
# function to sort array using selection sort
selection_sort <- function(x)
{
# length of array
n <- length(x)
for (i in 1 : (n - 1))
{
# assume element at i is minimum
min_index <- i
for (j in (i + 1) : (n))
{
# check if element at j is smaller than element at min_index
26
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
# take sample input
arr <- sample(1 : 100, 10)
# sort array
# print array
sorted_arr
Output:
[1] 18 30 46 51 58 64 76 80 91 93
27
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
14. Write an R program to implement the data structures
(i) Vectors (ii) Array (iii) Matrix (iv) Data Frame (v) Factors
(i) Vectors
R Program Code:
# R program to illustrate Vector
Output:
[1] 1 3 5 7 8
28
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
(ii) Array
R Program Code:
# R program to illustrate an array
A = array(
# Taking sequence of elements
c(1, 2, 3, 4, 5, 6, 7, 8),
# Creating two rectangular matrices each with two rows and two columns
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
29
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
(iii) Matrix
R Program Code:
# R program to illustrate a matrix
A = matrix(
# Taking sequence of elements
c(1, 2, 3, 4, 5, 6, 7, 8, 9),
print(A)
Output:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
30
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
(iv) Data Frame
R Program Code:
# R program to illustrate dataframe
# To create dataframe use data.frame command and then pass each of the vectors
print(df)
Output:
Name Language Age
1 Amiya R 22
2 Raj Python 25
3 Asish Java 45
31
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
(v) Factors
R Program Code:
# R program to illustrate factors
print(fac)
Output:
[1] Male Female Male Male Female Male Female
Levels: Female Male
32
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
15. Write a R program to implement scan(), merge(), read.csv() and read.table()
commands.
scan():
R Program Code:
# take input from keyboard
z <- scan()
1
2
3
4
# print output on the screen
print(z)
Output:
Read 4 items
[1] 1 2 3 4
33
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
merge()
R Program Code:
# R program to merge two data frames
Output:
row1 row2 col2
1 a d Hello
2 b e Hellos
3 c f gfg
34
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
read.csv()
R Program Code:
csv_data <- read.csv(file = 'sample.csv')
print(csv_data)
Output:
id, name, department, salary, projects
1 1 A HR 60754 14
2 2 B Tech 59640 3
3 3 C Marketing 69040 8
4 4 D HR 65043 5
5 5 E Tech 59943 2
6 6 F IT 65000 5
7 7 G HR 69000 7
[1] 4
[1] 7
35
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
read.table()
R Program Code:
table1 = read.table("Hello.txt")
print(table1)
Output:
a b
p 1 2
q 3 4
r 5 6
s 7 8
36
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
16. Write an R program to implement “Executing Scripts” written on the notepad, by
calling to the console.
R Program Code:
x=34
y=16
z=x+y
w=y/x
x
y
z
w
x="some text"
x
y
z
w
37
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
Output:
[1] 34
[1] 16
[1] 50
[1] 0.4705882
[1] "some text"
[1] 16
[1] 50
[1] 0.4705882
38
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
17. Write R program, Reading data from files and working with datasets
(i) Reading data from csv files, inspection of data.
(ii) Reading data from Excel files.
Output:
X id, name, department, salary, projects
1 4 4 D HR 65043 5
2 7 7 G HR 69000 7
39
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
(ii) Reading data from Excel files
R Program Code:
# Working with Excel Files
# Installing required package
install.packages("readxl")
Output:
40
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
18. Write R program to implement Graphs
(i) Basic high-level plots (ii) Modifications of scatter plots
(iii) Modification of histograms, parallel box plots.
Output:
41
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
R Program Code:
#Plotting with R programming
x = 0:10
y = 0:10
plot(x,y)
lines(x,y,col = "red")
Output:
42
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
(ii) Modifications of scatter plots
R Program Code:
set.seed(12)
n <- 100
x <- runif(n)
eps <- rnorm(n, 0, 0.25)
y <- 2 + 3 * x^2 + eps
plot(x, y, pch = 19, col = "black")
Output:
43
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
(iii) Modification of histograms, parallel box plots.
R Program Code:
# Create data for the graph.
v <- c(19, 23, 11, 5, 16, 21, 32, 14, 19, 27, 39)
Output:
44
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
R Program Code:
# Creating data for the graph.
v <- c(19, 23, 11, 5, 16, 21, 32, 14, 19, 27, 39, 120, 40, 70, 90)
# Setting labels
text(m$mids, m$counts, labels = m$counts, adj = c(0.5, -0.5))
Output:
45
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB