1.
To create an array of two 3 ^ * 3 matrices each with 3 rows and 3 columns from two given
two vectors. Print the second row of the second matrix of the array and the element in the
3rd row and 3rd column of the 1st matrix.
# Create the two vectors
vector1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
vector2 <- c(10, 11, 12, 13, 14, 15, 16, 17, 18)
# Create the array of matrices
array <- array(c(vector1, vector2), dim = c(3, 3, 2))
# Print the second row of the second matrix
print(array[2, , 2])
# Print the element in the third row and third column of the first matrix
print(array[3, 3, 1])
Output
> # Create the two vectors
> vector1 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
> vector2 <- c(10, 11, 12, 13, 14, 15, 16, 17, 18)
>
> # Create the array of matrices
> array <- array(c(vector1, vector2), dim = c(3, 3, 2))
>
> # Print the second row of the second matrix
> print(array[2, , 2])
[1] 11 14 17
>
> # Print the element in the third row and third column of the first
matrix
> print(array[3, 3, 1])
[1] 9
2. To create a data frame for four given vector.
Student_details<-data.frame(
Student_ID=c(100,100,101,102,103,104),
Student_Name=c("Shiny","Ruchir","Smily","Hansraj","Petrina","Sweety"),
Maths_Score=c(100,98,88,87,76,92),
Science_Score=c(97,99,74,82,65,83)
print(Student_details)
OUTPUT
> Student_details<-data.frame(
+ Student_ID=c(100,100,101,102,103,104),
+ Student_Name=c("Shiny","Ruchir","Smily","Hansraj","Petrina","Sweety"),
+ Maths_Score=c(100,98,88,87,76,92),
+ Science_Score=c(97,99,74,82,65,83)
+ )
> print(Student_details)
Student_ID Student_Name Maths_Score Science_Score
1 100 Shiny 100 97
2 100 Ruchir 98 99
3 101 Smily 88 74
4 102 Hansraj 87 82
5 103 Petrina 76 65
6 104 Sweety 92 83
3. To find the maximum and minimum value of a given vector
x<-c(12,55,76,89,20,30)
cat("Given Vector:",x,"\n")
cat("Max:",max(x),"\n")
cat("Min:",min(x),"\n")
OUTPUT
> x<-c(12,55,76,89,20,30)
> cat("Given Vector:",x,"\n")
Given Vector: 12 55 76 89 20 30
> cat("Max:",max(x),"\n")
Max: 89
> cat("Min:",min(x),"\n")
Min: 12
4. Generate 2 vector d1 and d2 with 10 element using seq (), c () function and perform the product
of both. show how the element can be included or deleted in the vector
# Generate vector d1 using seq() function
d1 <- seq(1, 10)
# Generate vector d2 using c() function
d2 <- c(11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
# Perform the product of both vectors
product <- d1 * d2
# Print the product
print(product)
# Include an element in the vector
d1 <- c(d1, 11)
# Delete an element from the vector
d2 <- d2[-5]
# Print the updated vectors
print(d1)
print(d2)
OUTPUT
> # Generate vector d1 using seq() function
> d1 <- seq(1, 10)
> # Generate vector d2 using c() function
> d2 <- c(11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
> # Perform the product of both vectors
> product <- d1 * d2
> # Print the product
> print(product)
[1] 11 24 39 56 75 96 119 144 171 200
> # Include an element in the vector
> d1 <- c(d1, 11)
> # Delete an element from the vector
> d2 <- d2[-5]
> # Print the updated vectors
> print(d1)
[1] 1 2 3 4 5 6 7 8 9 10 11
> print(d2)
[1] 11 12 13 14 16 17 18 19 20
>
4. To create a two dimensional 5 *3 array of sequences of even integers greater than 50.
a <- array(seq(from = 50, length.out = 15, by = 2), c(5, 3))
print("5×3 array of sequence of even integers greater than 50:")
print(a)
OUTPUT
> a <- array(seq(from = 50, length.out = 15, by = 2), c(5, 3))
> print("5×3 array of sequence of even integers greater than 50:")
[1] "5×3 array of sequence of even integers greater than 50:"
> print(a)
[,1] [,2] [,3]
[1,] 50 60 70
[2,] 52 62 72
[3,] 54 64 74
[4,] 56 66 76
[5,] 58 68 78
5. Generate an array of dimension 2 * 5 and repeat the same 3 times
# Create the initial array
initial_array <- array(1:(2*5), dim = c(2, 5))
# Repeat the array 3 times
repeated_array <- array(initial_array, dim = c(2, 5, 3))
# Print the repeated array
print(repeated_array)
OUTPUT
> # Create the initial array
> initial_array <- array(1:(2*5), dim = c(2, 5))
>
> # Repeat the array 3 times
> repeated_array <- array(initial_array, dim = c(2, 5, 3))
>
> # Print the repeated array
> print(repeated_array)
, , 1
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
, , 2
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
, , 3
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
7. To create a matrix from a list of given vectors and perform the transpose, multiplication.
# Create a list of vectors
vector1 <- c(1, 2, 3)
vector2 <- c(4, 5, 6)
vector3 <- c(7, 8, 9)
vector_list <- list(vector1, vector2, vector3)
# Create a matrix from the list of vectors
matrix_from_list <- do.call(rbind, vector_list)
# Transpose the matrix
transposed_matrix <- t(matrix_from_list)
# Perform matrix multiplication
multiplied_matrix <- matrix_from_list %*% transposed_matrix
# Print the results
print("Matrix from list:")
print(matrix_from_list)
print("Transposed matrix:")
print(transposed_matrix)
print("Multiplied matrix:")
print(multiplied_matrix)
OUTPUT
> # Create a list of vectors
> vector1 <- c(1, 2, 3)
> vector2 <- c(4, 5, 6)
> vector3 <- c(7, 8, 9)
> vector_list <- list(vector1, vector2, vector3)
>
> # Create a matrix from the list of vectors
> matrix_from_list <- do.call(rbind, vector_list)
>
> # Transpose the matrix
> transposed_matrix <- t(matrix_from_list)
>
> # Perform matrix multiplication
> multiplied_matrix <- matrix_from_list %*% transposed_matrix
>
> # Print the results
> print("Matrix from list:")
[1] "Matrix from list:"
> print(matrix_from_list)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
> print("Transposed matrix:")
[1] "Transposed matrix:"
> print(transposed_matrix)
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
> print("Multiplied matrix:")
[1] "Multiplied matrix:"
> print(multiplied_matrix)
[,1] [,2] [,3]
[1,] 14 32 50
[2,] 32 77 122
[3,] 50 122 194
>
8. To search the data array using binary search algorithm.
# Binary search function
binary_search <- function(arr, target) {
low <- 1
high <- length(arr)
while (low <= high) {
mid <- floor((low + high) / 2)
if (arr[mid] == target) {
return(mid) # Found the target
} else if (arr[mid] < target) {
low <- mid + 1 # Target is in the right half
} else {
high <- mid - 1 # Target is in the left half
return(-1) # Target not found
# Example usage
data_array <- c(1, 3, 5, 7, 9, 11, 13, 15)
target_value <- 9
# Call the binary search function
result <- binary_search(data_array, target_value)
# Print the result
if (result == -1) {
print("Target value not found in the data array.")
} else {
print(paste("Target value found at index", result))
OUTPUT
[1] "Target value found at index 5"
9. To find the Armstrong Number.
is_armstrong<-function(x)
t=x
s=0
while(t>0)
u=t%%10
s=s+(u^3)
t=floor(t/10)
if(s==x)
cat(x,"is an Armstrong Number\n")
else
cat(x,"is not an Armstrong Number\n")
}
n=153
is_armstrong(n)
m=122
is_armstrong(m)
OUTPUT
> n=153
> is_armstrong(n)
153 is an Armstrong Number
> m=122
> is_armstrong(m)
122 is not an Armstrong Number
10. To find a factorial of a number.
rec_fac<-function(x){
if(x==0||x==1){
return(1)
else{
return(x*rec_fac(x-1))
rec_fac(5)
OUTPUT
[1] 120
PRIME AND COMPOSITE NUMBERS
is_prime<-function(n){
if(n<=1) return(FALSE)
for(i in 2:(n)^0.5){
if(n%%i==0) return(FALSE)
}
return(TRUE)
v=seq(from=2,to=10)
p<-c()
com<-c()
for(i in v){
if(is_prime(i)){
p<-c(p,i)
else{
com<-c(com,i)
cat("Prime Numbers:",p,"\n")
cat("Composite Numbers",com,"\n")
OUTPUT
Prime Numbers: 3 5 7
Composite Numbers 2 4 6 8 9 10