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

M.tech - Data Science Lab

This document contains a lab manual for a Data Science lab course. It provides 17 experiments involving R programming concepts and tasks. The experiments cover topics such as basic R syntax, reading input, implementing functions, data structures, file input/output, and basic graphs. Suggested books and online resources are also provided for further study. The overall purpose of the lab course is for students to learn and apply concepts of big data and R programming to solve problems.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
668 views

M.tech - Data Science Lab

This document contains a lab manual for a Data Science lab course. It provides 17 experiments involving R programming concepts and tasks. The experiments cover topics such as basic R syntax, reading input, implementing functions, data structures, file input/output, and basic graphs. Suggested books and online resources are also provided for further study. The overall purpose of the lab course is for students to learn and apply concepts of big data and R programming to solve problems.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

MASTER OF TECHNOLOGY

COMPUTER SCIENCE & ENGINEERING


REGULATION: R19

LAB MANUAL
for
DATA SCIENCE LAB

VAAGESWARI COLLEGE OF ENGINEERING


BESIDE LMD POLICE STATION, RAMAKRISHNA COLONY
KARIMNAGAR - 505481

I
R19 M.TECH. CSE/CS

JAWAHARLAL NEHRU TECHNOLOGICAL UNIVERSITY HYDERABAD


M. Tech - CSE/CS – I Year – II Semester
Common to CSE and CS Syllabus

DATA SCIENCE LAB (Lab – IV)

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.

Suggested Books for Lab:


1. Big data – Black Book: 2015 edition: dreamtech press. Pg. (490- 642)

II
R19 M.TECH. CSE/CS

2. Introducing to programming and problem solving by scala, mark c. lewis, lisa


l.lacher. CRC press, second edition.

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:

1. Write an R program to evaluate the following expression ax+b/ax-b.

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();

# convert the inputted value to integer


var = as.integer(var);

# print the value


print(var)

Output:
255
[1] 255

(Or)

Input from the user


R program Code:
name = readline(prompt="Input your name: ")
age = readline(prompt="Input your age: ")

print(paste("My name is",name, "and I am",age ,"years old."))


print(R.version.string)

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: ")

Input your age: 12


> print(paste("My name is",name, "and I am",age ,"years old."))
[1] "My name is hello and I am 12 years old."
> print(R.version.string)
[1] "R version 4.1.1 (2021-08-16)"

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.

(i) Sum of all 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

(i) Total number of 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

(i) sum of two matrices A and B


R program Code:
# Create two 2x3 matrices.
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 addition")
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 addition"
[,1] [,2] [,3]
[1,] 1 5 5

[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:"

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


[1,] 0 2 0
[2,] 1 3 2
[1] "Result of subtraction"
[,1] [,2] [,3]
[1,] 1 1 5

[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: "))

num2 = as.integer(readline(prompt="Enter a number 2: "))


num3 = as.integer(readline(prompt="Enter a number 3: "))
num4 = as.integer(readline(prompt="Enter a number 4: "))
x = c(num1, num2, num3, num4)
print(sum(x))

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.

i) R Program to swap two integers without 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))
x=x+y
y=x-y
x=x-y
print(paste("After swap x is :", x))

print(paste("After swap y is :", y))

Output:
Enter x value :89

Enter y value :78


[1] "Before swap x is : 89"
[1] "Before swap y is : 78"

[1] "After swap x is : 78"


[1] "After swap y is : 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

Enter y value :57


[1] "Before swap x is : 87"
[1] "Before swap y is : 57"

[1] "After swap x is : 57"


[1] "After swap y is : 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.

i) R Program to Find the Factorial of a Number Using Recursive Function (Recursion)

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

# check is the number is negative, positive or zero


if(num < 0) {
print("Sorry, factorial does not exist for negative numbers")
} else if(num == 0) {
print("The factorial of 0 is 1")
} else {

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 if (table[m] < key) {


if (r == 1L) {
return(FALSE)
}
BiSearch(table[(m + 1L):r], key)
}

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

if (x[j] > x[j + 1]) {


temp <- x[j]
x[j] <- x[j + 1]
x[j + 1] <- temp
}
}

}
x
}

# take 10 random numbers between 1 - 100


arr <- sample(1 : 100, 10)

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

if (x[j] < x[min_index]) {


# if yes, update min_index
min_index = j
}
}
# swap element at i with element at min_index

temp <- x[i]


x[i] <- x[min_index]
x[min_index] <- temp
}
x
}

26
I M.Tech II Sem CSE/CS (R19 Regulation)-DATA SCIENCE LAB
# take sample input
arr <- sample(1 : 100, 10)

# sort array

sorted_arr <- selection_sort(arr)

# 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

# Vectors(ordered collection of same data type)


X = c(1, 3, 5, 7, 8)

# Printing those elements in console


print(X)

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),

# No of rows and columns


nrow = 3, ncol = 3,

# By default matrices are in column-wise order


# So this parameter decides how to arrange the matrix
byrow = TRUE

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

# A vector which is a character vector


Name = c("Amiya", "Raj", "Asish")

# A vector which is a character vector

Language = c("R", "Python", "Java")

# A vector which is a numeric vector


Age = c(22, 25, 45)

# To create dataframe use data.frame command and then pass each of the vectors

# we have created as arguments to the function data.frame()


df = data.frame(Name, Language, Age)

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

# Creating factor using factor()


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

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

# Creating data frames


df1 <- data.frame(row1 = c("a", "b", "c"), row2 = c("d", "e", "f"))
df2 <- data.frame(col1 = c("a", "b", "c"), col2 = c("Hello", "Hellos", "gfg"))

# Calling merge() function


df <- merge(df1, df2, by.x ="row1", by.y ="col1")
print(df)

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)

# print number of columns


print (ncol(csv_data))

# print number of rows


print(nrow(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.

(i) Reading data from csv files, inspection of data


R Program Code:
csv_data <- read.csv(file ='sample.csv')
new_csv <- subset(csv_data, department == "HR" & projects <10)
write.csv(new_csv, "new_sample.csv")
new_data <-read.csv(file ='new_sample.csv')
print(new_data)

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")

# Loading the package


library(readxl)

# Importing excel file


Data2 <- read_excel("Sample_data2.xlsx")

# Printing the data


head(Data2)

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.

(i) Basic high-level plots


R Program Code:
x = 0:10
y = 0:10
plot(x,y)

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")

plot(y ~ x, pch = 19, col = "black") # Equivalent

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)

# Create the histogram.


hist(v, xlab = "No.of Articles ", col = "green", border = "black")

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)

# Creating the histogram.


m<-hist(v, xlab = "Weight", ylab ="Frequency",
col = "darkmagenta", border = "pink", breaks = 5)

# 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy