R Extra Programs
R Extra Programs
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.
# Print the element in the third row and third column of the first matrix
print(array[3, 3, 1])
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)
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
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
d2 <- c(11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
product <- d1 * d2
print(product)
# Include an element in the vector
d2 <- d2[-5]
print(d1)
print(d2)
OUTPUT
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
, , 2
, , 3
7. To create a matrix from a list of given vectors and perform the transpose, multiplication.
print(matrix_from_list)
print("Transposed matrix:")
print(transposed_matrix)
print("Multiplied matrix:")
print(multiplied_matrix)
OUTPUT
low <- 1
if (arr[mid] == target) {
} else {
# Example usage
target_value <- 9
if (result == -1) {
} else {
OUTPUT
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)
else
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
rec_fac<-function(x){
if(x==0||x==1){
return(1)
else{
return(x*rec_fac(x-1))
rec_fac(5)
OUTPUT
[1] 120
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