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

r Programming Lab Manual

The document is a lab manual for the R Programming Lab at Rungta College of Engineering & Technology for the 8th semester. It includes a list of DOs and DON’Ts for laboratory conduct, along with a detailed list of experiments prescribed by the university syllabus, including sample codes and expected outputs for each experiment. The manual is prepared by Prof. Anmol Agrawal and is managed by the Santosh Rungta Group of Institutions.

Uploaded by

Gangesh Sawarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

r Programming Lab Manual

The document is a lab manual for the R Programming Lab at Rungta College of Engineering & Technology for the 8th semester. It includes a list of DOs and DON’Ts for laboratory conduct, along with a detailed list of experiments prescribed by the university syllabus, including sample codes and expected outputs for each experiment. The manual is prepared by Prof. Anmol Agrawal and is managed by the Santosh Rungta Group of Institutions.

Uploaded by

Gangesh Sawarkar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

RUNGTA COLLEGE OF ENGINEERING & TECHNOLOGY

DEPARTMENT OF Computer Science and Engineering

LAB MANUAL

R Programming LAB

SEMESTER 8th

RUNGTA COLLEGE
Rungta Educational Campus,
Kohka-Kurud Road, Bhilai,
Chhattisgarh, India
Phone No. 0788-6666666
MANAGED BY : SANTOSH RUNGTA GROUP OF INSTITUTIONS

Prepared By: Prof. Anmol Agrawal


RUNGTA COLLEGE OF ENGINEERING & TECHNOLOGY
DEPARTMENT OF Computer Science and Engineering

LAB MANUAL
R Programming LAB
SEMESTER 8th

PREPARED AS PER THE SYLLABUS PRESCRIBED BY

CHHATTISGARH SWAMI VIVEKANAND TECHNICAL UNIVERSITY, BHILAI


List of DOs & DON’Ts.

(Give instructions as per < Name Of The Department > Laboratories)

DOs:

▪ Remove your shoes outside the laboratory.


▪ Come prepared in the lab regarding the experiment to be performed in the

lab.

▪ Take help from the Manual / Work Book for preparation of the experiment.

▪ For any abnormal working of the machine consult the Faculty In-charge/

Lab Assistant.

▪ Shut down the machine and switch off the power supply after performing the

experiment.

▪ Maintain silence and proper discipline in the lab.

▪ Enter your machine number in the Login register.

DON’Ts:

▪ Do not bring any magnetic material into the lab.

▪ Do not eat or drink anything in the lab.

▪ Do not tamper with the instruments in the Lab and do not disturb their

settings.
LIST OF EXPERIMENTS
AS PER THE SYLLABUS PRESCRIBED BY THE UNIVERSITY
LIST OF EXPERIMENTS
AS PER RUNGTA COLLEGE OF ENGINEERING & TECHNOLOGY

ExpNo Program PAGE LO


. NO.

1 Write an R program to create a sequence of 7-8 1&2


numbers from 20 to 50 and find the mean of
numbers from 20 to 60 and sum of numbers from
51 to 91
2 Write a program to find sum, mean and product 9-10 2
of vectors and avoid na , nan
3 Write a program to check whether a year (integer) 11 3
entered by the user is a leap year or not?
4 Write an R program to find the sum of natural 12-13 2
numbers without formula using the if–else
statement and the while loop
5 Write a program that prints the grades of the 14-15 2
students according to the marks obtained. The
grading of the marks should be as follows. Marks
Grades
800-1000 A+ 700 – 800 A 500 – 700 B+ 400-
500 B 150 – 400 C Less than 150 D.
6 Write an R program to make a simple calculator 16-18 1, 2,
that can add, subtract, multiply and divide using &3
switch cases and functions.
7 Write a program to perform searching within a 19 2&3
list (1 to 50). If the number is found in the list,
print that the search is successful otherwise print
that the number is not in the list.
8 Create a list and data frame that stores the marks 20-21 1&2
of any three subjects for 10 students. Find out the
total marks, average, maximum marks and
minimum marks of every subject.
9 Write a program to create two 3 X 3 matrices A 22-23 1&2
and B and perform the following operations
a) Transpose of the matrix
b) addition
c) subtraction.
10 Write an R program to create a list containing 24-25 2
strings, numbers, vectors and logical values and
do the following manipulations over the list.
a. Access the first element in the list
b. Give the names of the elements in the list
c. Add an element at some position in the list
d. Remove the element
e. Print the fourth element
f. Update the third element
11 Write an R program 26-27 4
a. To create a simple bar plot of five subject
marks.
b. Create a bell curve of a random normal
distribution
Experiment No. 1

Aim: Write an R program to create a sequence of numbers from


20 to 50 and find the mean of numbers from 20 to 60 and sum of
numbers from 51 to 91

Source Code:
# Create a sequence of numbers from 20 to 50
sequence_20_to_50 <- 20:50

# Find the mean of numbers from 20 to 60


mean_20_to_60 <- mean(20:60)

# Find the sum of numbers from 51 to 91


sum_51_to_91 <- sum(51:91)

# Print the results


print("Sequence of numbers from 20 to 50:")
print(sequence_20_to_50)

print("Mean of numbers from 20 to 60:")


print(mean_20_to_60)

print("Sum of numbers from 51 to 91:")


print(sum_51_to_91)

Outputs :
> source("D:/R New/Experiment-1.R")

[1] "Sequence of numbers from 20 to 50:"

[1] 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
43

[25] 44 45 46 47 48 49 50

[1] "Mean of numbers from 20 to 60:"

[1] 40

[1] "Sum of numbers from 51 to 91:"


[1] 2911

Experiment No. 2

AIM: Write a program to find sum, mean and product of


vectors and avoid na , nan
Source Code:
# Sample vectors
vector1 <- c(1, 2, NA, 4, 5, NaN)
vector2 <- c(3, 2, 1, NA, NaN, 6)
# Function to calculate sum, mean, and product of
vectors while avoiding NA and NaN

calculate_stats <- function(vector) {


clean_vector <- vector[!is.na(vector) &
!is.nan(vector)]
c(sum = sum(clean_vector), mean =
mean(clean_vector), product = prod(clean_vector))
}
# Calculate stats for vector1
stats_vector1 <- calculate_stats(vector1)
# Calculate stats for vector2
stats_vector2 <- calculate_stats(vector2)

# Print results
print(stats_vector1)
print(stats_vector2)

Output:

> source("D:/R New/Experiment-2.R")

sum mean product

12 3 40
sum mean product
12 3 36

EXPERIMENT No. 3

AIM: Write a program to check whether a year (integer) entered


by the user is a leap year or not?

Source Code:

# Function to check if a year is a leap year


is_leap_year <- function(year) {
# Leap years are divisible by 4
if (year %% 4 == 0) {
# Unless they are also divisible by 100
if (year %% 100 == 0) {
# Except if they are also divisible by 400
if (year %% 400 == 0) {
return(TRUE)
} else {
return(FALSE)
}
} else {
return(TRUE)
}
} else {
return(FALSE)
}
}

# Prompt the user to enter a year


year <- as.integer(readline(prompt = "Enter a year: "))

# Check if the entered year is a leap year


if (is_leap_year(year)) {
cat(year, "is a leap year.\n")
} else {
cat(year, "is not a leap year.\n")
}

Output:
source("D:/R New/Experiment-3.R")
Enter a year: 2000
2000 is a leap year.
> source("D:/R New/Experiment-3.R")
Enter a year: 2010
2010 is not a leap year.

EXPERIMENT No.4
Aim:-Write an R program to find the sum of natural numbers
without formula using the if–else statement and the while loop

Source code:
# Function to calculate the sum of natural numbers up to n
sum_of_natural_numbers <- function(n) {
# Initialize variables
i <- 1
sum <- 0

# Iterate through natural numbers up to n


while (i <= n) {
# Add current number to sum
sum <- sum + i
# Increment the counter
i <- i + 1
}

# Return the sum


return(sum)
}

# Prompt the user to enter a number


n <- as.integer(readline(prompt = "Enter a natural number: "))

# Check if the entered number is positive


if (n >= 0) {
# Calculate and print the sum of natural numbers up to n
cat("The sum of natural numbers up to", n, "is:",
sum_of_natural_numbers(n), "\n")
} else {
cat("Please enter a non-negative number.\n")
}

Output:
> source("D:/R New/Experiment-4.R")
Enter a natural number: 6
The sum of natural numbers up to 6 is: 21
EXPERIMENT No. 5
Aim: - Write a program that prints the grades of the students
according to the marks obtained. The grading of the marks should be
as follows. Marks Grades
800-1000 A+ 700 – 800 A 500 – 700 B+ 400-500 B 150 – 400 C Less than
150 D.

Source code:
# Function to determine the grade based on marks
get_grade <- function(marks) {
if (marks >= 800 & marks <= 1000) {
return("A+")
} else if (marks >= 700 & marks < 800) {
return("A")
} else if (marks >= 500 & marks < 700) {
return("B+")
} else if (marks >= 400 & marks < 500) {
return("B")
} else if (marks >= 150 & marks < 400) {
return("C")
} else {
return("D")
}
}

# Example marks of students


student_marks <- c(820, 750, 600, 480, 250)

# Print grades for each student


cat("Grades:\n")
for (marks in student_marks) {
cat("Marks:", marks, "- Grade:", get_grade(marks), "\n")
}

Output:

source("D:/R New/Experiment-5.R")
Grades:
Marks: 820 - Grade: A+
Marks: 750 - Grade: A
Marks: 600 - Grade: B+
Marks: 480 - Grade: B
Marks: 250 - Grade: C
EXPERIMENT No.6
Aim:-Write an R program to make a simple calculator that can
add, subtract, multiply and divide using switch cases and
functions.
Source code:
# Function to add two numbers
add <- function(a, b) {
return(a + b)
}

# Function to subtract two numbers


subtract <- function(a, b) {
return(a - b)
}

# Function to multiply two numbers


multiply <- function(a, b) {
return(a * b)
}

# Function to divide two numbers


divide <- function(a, b) {
if (b == 0) {
return("Cannot divide by zero!")
} else {
return(a / b)
}
}

# Main function to perform calculations based on user input


calculator <- function() {
cat("Welcome to Simple Calculator in R!\n")
cat("Enter the operation you want to perform:\n")
cat("1. Add\n")
cat("2. Subtract\n")
cat("3. Multiply\n")
cat("4. Divide\n")

choice <- as.integer(readline(prompt = "Enter your choice


(1-4): "))

switch(choice,
"1" = {
a <- as.numeric(readline(prompt = "Enter the first
number: "))
b <- as.numeric(readline(prompt = "Enter the second
number: "))
result <- add(a, b)
cat("Result:", result, "\n")
},
"2" = {
a <- as.numeric(readline(prompt = "Enter the first
number: "))
b <- as.numeric(readline(prompt = "Enter the second
number: "))
result <- subtract(a, b)
cat("Result:", result, "\n")
},
"3" = {
a <- as.numeric(readline(prompt = "Enter the first
number: "))
b <- as.numeric(readline(prompt = "Enter the second
number: "))
result <- multiply(a, b)
cat("Result:", result, "\n")
},
"4" = {
a <- as.numeric(readline(prompt = "Enter the first
number: "))
b <- as.numeric(readline(prompt = "Enter the second
number: "))
result <- divide(a, b)
cat("Result:", result, "\n")
},
{
cat("Invalid choice!\n")
}
)
}

# Call the calculator function


calculator()

Output:
source("D:/R New/Experiment-6.R")

Welcome to Simple Calculator in R!

Enter the operation you want to perform:

1. Add

2. Subtract

3. Multiply

4. Divide

Enter your choice (1-4): 4

Enter the first number: 7

Enter the second number: 3


Result: 2.333333
EXPERIMENT No.7
Aim: -Write a program to perform searching within a list (1 to
50). If the number is found in the list, print that the search is
successful otherwise print that the number is not in the list.
Source code:
# List of numbers from 1 to 50
number_list <- 1:50

# Function to perform searching within the list


search_number <- function(number, list) {
if (number %in% list) {
cat("Search successful: Number", number, "is in the list.\
n")
} else {
cat("Search unsuccessful: Number", number, "is not in the
list.\n")
}
}

# Example: Search for the number 25


search_number(25, number_list)

# Example: Search for the number 60


search_number(60, number_list)

Output:
source("D:/R New/experiment-7.R")
Search successful: Number 25 is in the list.
Search unsuccessful: Number 60 is not in the list.
EXPERIMENT No. 8
Aim: - Create a list and data frame that stores the marks of any
three subjects for 10 students. Find out the total marks, average,
maximum marks and minimum marks of every subject.

Source code:

# Create a data frame to store marks of three subjects for 10


students
marks_df <- data.frame(
Student = paste("Student", 1:10, sep = ""),
Subject1 = c(85, 76, 82, 91, 78, 86, 75, 90, 83, 88),
Subject2 = c(90, 88, 79, 85, 80, 92, 83, 88, 86, 90),
Subject3 = c(78, 92, 85, 90, 75, 89, 79, 87, 80, 84)
)

# Calculate total, average, maximum, and minimum marks for


each subject
subject_stats <- data.frame(
Subject = c("Subject1", "Subject2", "Subject3"),
Total = colSums(marks_df[, -1]),
Average = colMeans(marks_df[, -1]),
Maximum = sapply(marks_df[, -1], max),
Minimum = sapply(marks_df[, -1], min)
)

# Print the data frame showing statistics for each subject


print(subject_stats)
Output:
source("D:/R New/Experiment-8.R")
Subject Total Average Maximum Minimum
Subject1 Subject1 834 83.4 91 75
Subject2 Subject2 861 86.1 92 79
Subject3 Subject3 839 83.9 92 75
EXPERIMENT No.9

Aim:-Write a program to create two 3 X 3 matrices A and B


and perform the following operations
a) Transpose of the matrix
b) addition
c) subtraction.

Source code:

# Create matrix A
A <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, byrow =
TRUE)
cat("Matrix A:\n")
print(A)

# Create matrix B
B <- matrix(c(9, 8, 7, 6, 5, 4, 3, 2, 1), nrow = 3, byrow =
TRUE)
cat("\nMatrix B:\n")
print(B)

# Transpose of matrices
cat("\nTranspose of Matrix A:\n")
print(t(A))
cat("\nTranspose of Matrix B:\n")
print(t(B))

# Addition of matrices
cat("\nAddition of Matrix A and Matrix B:\n")
print(A + B)

# Subtraction of matrices
cat("\nSubtraction of Matrix A from Matrix B:\n")
print(B - A)
Output:
source("D:/R New/Experiment-9.R")
Matrix A:
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9

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

Transpose of Matrix A:
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9

Transpose of Matrix B:
[,1] [,2] [,3]
[1,] 9 6 3
[2,] 8 5 2
[3,] 7 4 1

Addition of Matrix A and Matrix B:


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

Subtraction of Matrix A from Matrix B:


[,1] [,2] [,3]
[1,] 8 6 4
[2,] 2 0 -2
[3,] -4 -6 -8

EXPERIMENT No. 10

Aim:-Write an R program to create a list containing strings,


numbers, vectors and logical values and do the following
manipulations over the list.
a. Access the first element in the list
b. Give the names of the elements in the list
c. Add an element at some position in the list
d. Remove the element
e. Print the fourth element
f. Update the third element

Source Code:-

# Create a list containing strings, numbers, vectors, and


logical values
my_list <- list(
strings = "Hello",
numbers = 123,
vector = c(1, 2, 3),
logical_value = TRUE
)

# a. Access the first element in the list


cat("First element in the list:", my_list[[1]], "\n")

# b. Give the names of the elements in the list


cat("Names of elements in the list:", names(my_list), "\n")

# c. Add an element at some position in the list


my_list[["new_element"]] <- "New Element"
cat("List after adding a new element:", "\n")
print(my_list)

# d. Remove the element


my_list[["new_element"]] <- NULL
cat("List after removing the new element:", "\n")
print(my_list)

# e. Print the fourth element


cat("Fourth element in the list:", my_list[[4]], "\n")

# f. Update the third element


my_list[[3]] <- c(4, 5, 6)
cat("List after updating the third element:", "\n")
print(my_list)

Output:
source("D:/R New/Experiment-10.R")
First element in the list: Hello
Names of elements in the list: strings numbers vector logical_value
List after adding a new element:
$strings
[1] "Hello"

$numbers
[1] 123

$vector
[1] 1 2 3

$logical_value
[1] TRUE

$new_element
[1] "New Element"

List after removing the new element:


$strings
[1] "Hello"

$numbers
[1] 123

$vector
[1] 1 2 3

$logical_value
[1] TRUE

Fourth element in the list: TRUE


List after updating the third element:
$strings
[1] "Hello"

$numbers
[1] 123

$vector
[1] 4 5 6

$logical_value
[1] TRUE

EXPERIMENT No. 11

Aim:- Write an R program

a. To create a simple bar plot of five subject marks.

b. Create a bell curve of a random normal distribution


Source Code:- 11.a. To create a simple bar plot of five subject
marks.
# Subject marks
subject_marks <- c(85, 72, 90, 78, 88)

# Subjects
subjects <- c("Math", "Science", "English", "History",
"Computer")

# Create a bar plot


barplot(subject_marks, names.arg = subjects, xlab =
"Subjects", ylab = "Marks", col = "skyblue", main = "Subject
Marks")

Output:

Source Code:- 11.b Create a bell curve of a random normal


distribution

# Generate random data following a normal distribution

random_data <- rnorm(1000, mean = 0, sd = 1)

# Create a bell curve

hist(random_data, breaks = 30, col = "lightblue", main = "Bell


Curve of Random Normal Distribution", xlab = "Values", ylab =
"Frequency", prob = TRUE)

curve(dnorm(x, mean = mean(random_data), sd =


sd(random_data)), add = TRUE, col = "red", lwd = 2)

Output:

You might also like