R programming (3)
R programming (3)
Signature :
INDEX
S.No LIST OF EXPERIMENT DATE PAGE REMARK
No.
I Write an R program for "Hello Sage". 13/01/2025 1
Output:-
1
Experiment-II
Write an R program to Add two vectors.
Code:-
vector1 <- c(10, 20, 30) # Define the first vector
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_product <- 1
for (val in vec) { # Loop through each element
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
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
Code:-
normal_numbers <- rnorm(5, mean = 0, sd = 1) # Normal distribution
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
for (num in numbers) { # Loop through the numbers to find min and max
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
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
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
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
while (temp > 0) { # Loop to extract digits and calculate sum of powers
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
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
print(list1)
cat("\nOriginal List 2:\n")
print(list2)
merged_list <- c(list1, list2) # Merge the two lists
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
print(my_list)
my_vector <- unlist(my_list) # Convert the list to a 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
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
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
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
print(list1)
cat("\nList 2:\n")
print(list2)
merged_vector <- unlist(c(list1, list2)) # Merge the two lists and convert to a 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
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