r Programming Lab Manual
r Programming Lab Manual
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
LAB MANUAL
R Programming LAB
SEMESTER 8th
DOs:
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.
DON’Ts:
▪ 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
Source Code:
# Create a sequence of numbers from 20 to 50
sequence_20_to_50 <- 20:50
Outputs :
> source("D:/R New/Experiment-1.R")
[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] 40
Experiment No. 2
# Print results
print(stats_vector1)
print(stats_vector2)
Output:
12 3 40
sum mean product
12 3 36
EXPERIMENT No. 3
Source Code:
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
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")
}
}
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)
}
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")
}
)
}
Output:
source("D:/R New/Experiment-6.R")
1. Add
2. Subtract
3. Multiply
4. Divide
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:
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
EXPERIMENT No. 10
Source Code:-
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"
$numbers
[1] 123
$vector
[1] 1 2 3
$logical_value
[1] TRUE
$numbers
[1] 123
$vector
[1] 4 5 6
$logical_value
[1] TRUE
EXPERIMENT No. 11
# Subjects
subjects <- c("Math", "Science", "English", "History",
"Computer")
Output:
Output: