SAGE UNIVERSITY, INDORE
Institute of Advance Computing
Laboratory Manual
B. Tech- Practical
Academic Session: Jan-Jun 2025
Name :Yuvraj Tomar
Enroll number : 22ADV3CSE1089
Program : B.Tech CSE
Course Code : ECSDSRPR001P
Course Name : R Programming Lab
Course Coordinator Head of Department
Name : Dr.Nirupma Tiwari Name : Dr. Manoj K. Ramaiya
Signature :
INDEX
S.No LIST OF EXPERIMENT DATE PAGE REMARK
No.
I Write an R program for "Hello Sage". 13/01/2025 1
II Write an R program to Add two vectors. 13/01/2025 2
III Find the Sum, Mean, and Product of the 16/01/2025 3
Vector in R programming.
IV Create an R program to take Input from the 16/01/2025 4
user.
V How to generate random numbers from 20/01/2025 5-6
standard distributions in R.
VI Create an R program to find the Minimum 23/01/2025 7
and Maximum.
VII R Program to Sort a Vector. 27/01/2025 8
VIII How to find the factorial of a number. 07/02/2025 9
IX How to create multiplication table of given 07/02/2025 10
number.
X Write an R program to check prime number. 10/02/2025 11
XI R program to check armstrong number. 13/02/2025 12
XII R program to print the fibonacci sequence. 17/02/2025 13
XIII Write a R program to create a list containing a 17/02/2025 14
vector, a matrix and a list and remove the
second element.
XIV Write a R program to merge two given lists 20/02/2025 15
into one list.
XV Write a R program to convert a given list to 24/02/2025 16
vector.
XVI Write a R program to convert a given matrix 03/03/2025 17
to a list.
XVII Write a R program to Add 10 to each element 06/03/2025 18
of the first vector in a given list.
XVIII Write a R program to merge two given lists 10/03/2025 19
into one Vector.
XIX Write a R program to list containing a vector, 27/03/2025 20
a matrix and a list and give names to the
elements in the list.
XX Write a R program to create a list containing 07/04/2025 21
strings, numbers, vectors and a logical values.
Experiment-I
Write an R program for "Hello Sage".
Code:-
cat("Hello Sage\n") # Print message
Output:-
1
Experiment-II
Write an R program to Add two vectors.
Code:-
vector1 <- c(10, 20, 30) # Define the first vector
vector2 <- c(1, 2, 3) # Define the second vector
result <- vector1 + vector2 # Add the two vectors
cat("Vector 1:", vector1, "\n") # Display the result
cat("Vector 2:", vector2, "\n")
cat("Sum of the two vectors:", result, "\n")
Output:-
2
Experiment-III
Find the Sum, Mean, and Product of the Vector in R
programming.
Code:-
vec <- c(2, 4, 6, 8) # Define a numeric vector
vec_sum <- 0 # Initialize variables
vec_product <- 1
for (val in vec) { # Loop through each element
vec_sum <- vec_sum + val
vec_product <- vec_product * val
}
vec_mean <- vec_sum / length(vec) # Calculate mean
cat("Sum:", vec_sum, "\n") # Display the results
cat("Mean:", vec_mean, "\n")
cat("Product:", vec_product, "\n")
Output:-
3
Experiment-IV
Create an R program to take Input from the user.
Code:-
a <- as.numeric(readline("Enter first number: ")) # Take input from user
b <- as.numeric(readline("Enter second number: "))
sum <- a + b # Calculate sum
cat("Sum:", sum, "\n") # Display result
Output:-
4
Experiment-V
How to generate random numbers from standard distributions.
In R, you can easily generate random numbers from standard distributions using
built-in functions. Here's a simple guide:
Common Standard Distributions in R
Distribution Function Example (generate 5 numbers)
Normal rnorm(n, mean, sd) rnorm(5, mean=0, sd=1)
Uniform runif(n, min, max) runif(5, min=0, max=1)
Binomial rbinom(n, size, prob) rbinom(5, size=10, prob=0.5)
Poisson rpois(n, lambda) rpois(5, lambda=3)
Exponential rexp(n, rate) rexp(5, rate=1)
Code:-
normal_numbers <- rnorm(5, mean = 0, sd = 1) # Normal distribution
cat("Normal Distribution:\n", normal_numbers, "\n\n")
uniform_numbers <- runif(5, min = 0, max = 1) # Uniform distribution
cat("Uniform Distribution:\n", uniform_numbers, "\n\n")
binomial_numbers <- rbinom(5, size = 10, prob = 0.5) # Binomial distribution
cat("Binomial Distribution:\n", binomial_numbers, "\n\n")
poisson_numbers <- rpois(5, lambda = 3) # Poisson distribution
cat("Poisson Distribution:\n", poisson_numbers, "\n\n")
exponential_numbers <- rexp(5, rate = 1) # Exponential distribution
cat("Exponential Distribution:\n", exponential_numbers, "\n")
5
Output:-
6
Experiment-VI
Create an R program to find the Minimum and Maximum.
Code:-
user_input <- readline("Enter numbers separated by commas: ")
split_input <- strsplit(user_input, ",")[[1]] # Split by comma
numbers <- as.numeric(split_input) # Convert to numeric
min_value <- numbers[1] #Initialize min with the first element
max_value <- numbers[1] #Initialize max with the first element
for (num in numbers) { # Loop through the numbers to find min and max
if (num < min_value) {
min_value <- num
}
if (num > max_value) {
max_value <- num
}
}
cat("Minimum value is:", min_value, "\n") # Display results
cat("Maximum value is:", max_value, "\n")
Output:-
7
Experiment-VII
R Program to Sort a Vector.
Code:-
user_input <- readline("Enter numbers separated by commas: ")
split_input <- strsplit(user_input, ",")[[1]] # Convert input to numeric vector
vec <- as.numeric(split_input)
n <- length(vec) # Get length of vector
for (i in 1:(n-1)) { # Bubble sort algorithm
for (j in 1:(n-i)) {
if (vec[j] > vec[j+1]) {
# Swap values
temp <- vec[j]
vec[j] <- vec[j+1]
vec[j+1] <- temp
}
}
}
cat("Sorted Vector:", vec, "\n") # Print sorted vector
Output:-
8
Experiment-VIII
How to find the factorial of a number.
Code:-
num <- as.integer(readline("Enter a number: "))
if (num < 0) { # Check for valid input
cat("Factorial is not defined for negative numbers.\n")
} else if (num == 0) {
cat("Factorial of 0 is 1\n")
} else {
fact <- 1 # Initialize result
for (i in 1:num) { # Loop to calculate factorial
fact <- fact * i
}
cat("Factorial of", num, "is", fact, "\n") # Print the result
}
Output:-
9
Experiment-IX
How to create multiplication table of given number.
Code:-
num <- as.integer(readline("Enter a number: "))
cat("Multiplication Table of", num, ":\n") # Print multiplication table up to 10
for (i in 1:10) {
result <- num * i
cat(num, "x", i, "=", result, "\n")
}
Output:-
10
Experiment-X
Write an R program to check prime number .
Code:-
num <- as.integer(readline("Enter a number: ")) # Take input from user
if (num <= 1) { # Check for prime
cat(num, "is not a prime number.\n")
} else {
is_prime <- TRUE
for (i in 2:(num - 1)) {
if (num %% i == 0) {
is_prime <- FALSE
break
}
}
if (is_prime) {
cat(num, "is a prime number.\n")
} else {
cat(num, "is not a prime number.\n")
}}
Output:-
11
Experiment-XI
R program to check armstrong number.
Code:-
num <- as.integer(readline("Enter a number: "))
original_num <- num # Store original number for later comparison
num_digits <- nchar(as.character(num)) # Calculate number of digits
sum <-0 # Initialize sum
temp <- num # Temporary variable for calculation
while (temp > 0) { # Loop to extract digits and calculate sum of powers
digit <- temp %% 10
sum <- sum + digit^num_digits
temp <- temp %/% 10
}
if (sum == original_num) { # Check if it's an Armstrong number
cat(original_num, "is an Armstrong number.\n")
} else {
cat(original_num, "is not an Armstrong number.\n")
}
Output:-
12
Experiment-XII
R program to print the fibonacci sequence.
Code:-
n <- as.integer(readline("Enter the number of terms for Fibonacci sequence: "))
a <- 0 # Initialize the first two terms
b <- 1
cat("Fibonacci Sequence:\n") # Print the Fibonacci sequence
if (n >= 1) {
cat(a, " ")
}
if (n >= 2) {
cat(b, " ")
}
for (i in 3:n) {
next_term <- a + b
cat(next_term, " ")
a <- b # Update a and b for next iteration
b <- next_term
}
cat("\n")
Output:-
13
Experiment-XIII
Write a R program to create a list containing a vector, a matrix
and a list and remove the second element.
Code:-
my_vector <- c(1, 2, 3, 4) # Create a vector, matrix, and list
my_matrix <- matrix(1:9, nrow = 3, ncol = 3)
my_list <- list(a = 10, b = 20, c = 30)
my_list1 <- list(my_vector, my_matrix, my_list) # Create a list
cat("Original List:\n")# Print the original list
print(my_list1)
my_list1 <- my_list1[-2] # Remove the second element from the list
cat("\nModified List (after removing second element):\n") # Print the modified list
print(my_list1)
Output:-
14
Experiment-XIV
Write a R program to merge two given lists into one list.
Code:-
list1 <- list(a = 10, b = 20, c = 30) # Create two lists
list2 <- list(d = 40, e = 50, f = 60)
cat("Original List 1:\n") # Print original lists
print(list1)
cat("\nOriginal List 2:\n")
print(list2)
merged_list <- c(list1, list2) # Merge the two lists
cat("\nMerged List:\n") # Print the merged list
print(merged_list)
Output:-
15
Experiment-XV
Write a R program to convert a given list to vector
Code:-
my_list <- list(10, 20, 30, 40, 50) # Create a list
cat("Original List:\n") # Print original list
print(my_list)
my_vector <- unlist(my_list) # Convert the list to a vector
cat("\nConverted Vector:\n") # Print the converted vector
print(my_vector)
Output:-
16
Experiment-XVI
Write a R program to convert a given matrix to a list.
Different way to convert the matrix into list
Your Goal Code Example
Each value becomes a list item as.list(my_matrix)
Each column becomes a list item split(my_matrix, col(my_matrix))
Each row becomes a list item split(my_matrix, row(my_matrix))
Code:-
my_matrix <- matrix(1:6, nrow = 2, ncol = 3) # Create a matrix
cat("Original Matrix:\n") # Print the original matrix
print(my_matrix)
my_list <- as.list(my_matrix) # Convert the matrix to a list
cat("\nConverted List:\n") # Print the converted list
print(my_list)
Output:-
17
Experiment-XVII
Write a R program to Add 10 to each element of the first vector in
a given list.
Code:-
my_list <- list(c(1, 2, 3), c(4, 5, 6), c(7, 8, 9)) # Create a list with vectors
cat("Original List:\n")# Print original list
print(my_list)
my_list[[1]] <- my_list[[1]] + 10 # Add 10 to each element of the first vector
cat("\nUpdated List (after adding 10 to first vector):\n") # Print updated list
print(my_list)
Output:-
18
Experiment-XVIII
Write a R program to merge two given lists into one Vector.
Code:-
list1 <- list(10, 20, 30) # Create two lists
list2 <- list(40, 50, 60)
cat("List 1:\n") # Print the original lists
print(list1)
cat("\nList 2:\n")
print(list2)
merged_vector <- unlist(c(list1, list2)) # Merge the two lists and convert to a vector
cat("\nMerged Vector:\n") # Print the merged vector
print(merged_vector)
Output:-
19
Experiment-XIX
Write a R program to list containing a vector, a matrix and a list
and give names to the elements in the list.
Code:-
my_vector <- c(1, 2, 3) # Create a vector
my_matrix <- matrix(1:4, nrow = 2) # Create a matrix
my_inner_list <- list("a", "b", "c") # Create a list
my_list <- list(my_vector, my_matrix, my_inner_list) # Create a list
names(my_list) <- c("Vector", "Matrix", "InnerList") # names of elements in thelist
print(my_list) # Print the final list
Output:-
20
Experiment-XX
Write a R program to create a list containing strings, numbers,
vectors and a logical values.
Code:-
my_string <- "Hello, R!" # Create individual elements
my_number <- 42
my_vector <- c(1, 2, 3, 4, 5)
my_logical <- TRUE
my_list <- list(my_string, my_number, my_vector, my_logical)#list with all elements
names(my_list) <- c("String", "Number", "Vector", "Logical") #Name of list
print(my_list) # Print the list
Output:-
21