okram
#add ,sub ,Mul div
v1=c(10,20,30,40)
v2=c(50,60,70,80)
add=v1+v2
cat("add",add)
Function to calculate and print the multiplication table
table<-function(num){
for(i in 1:10){
cat(i*num," ")
}
}
table(10)
Write a R program to reverse a number and also calculate the sum of digits of #that number
n=567
Reverse=function(n)
{
sum=0
rev=0
while(n>0)
{
r=n%%10
sum=sum+r
rev=rev*10+r
n=n%/%10
}
print(rev)
print(sum)
}
Reverse(n)
Function to calculate the sum of two matrices
Example matrices
matrix1 <- matrix(1:6)
matrix2 <- matrix(7:12)
sum1 = matrix1+matrix2
cat(sum1)
#Slip no: 6
Q1. Write a R program to create a data frame using two given vectors and display the duplicate
elements.
companies <- data.frame(Shares = c("TCS", "Reliance", "HDFC Bank", "Infosys",
"Reliance"),
Price = c(3200, 1900, 1500, 2200, 1900))
cat("After removing Duplicates ", "\n")
companies[duplicated(companies),]
#Slip no:7
Q1. Write a R program to create a sequence of numbers from 20 to 50 and find the mean of
numbers from 20 to 60 and sum of numbers from 51 to 91.
Answer:
print("Sequence of numbers from 20 to 50:")
print(seq(20,50))
print("Mean of numbers from 20 to 60:")
print(mean(20:60))
print("Sum of numbers from 51 to 91:")
print(sum(51:91))
#Slip no: 8
Q1. Write a R program to get the first 10 Fibonacci numbers.
Initialize variables for the first two Fibonacci numbers
fibonacci <- numeric(10)
fibonacci[1] <- 0
fibonacci[2] <- 1
Generate the remaining Fibonacci numbers
for (i in 3:10) {
fibonacci[i] <- fibonacci[i - 1] + fibonacci[i - 2]
}
Display the result
print(fibonacci)
#Slip no:9
Q1. Write an R program to create a Data frames which contain details of 5 employees and display
summary of the data
Employees = data.frame(Name=c("Amit S","Dikisha R","Shweta J", "Jikita A","Riya M"),
Gender=c("M","M","F","F","F"),
Age=c(23,22,25,26,32),
Designation=c("Clerk","Manager","Exective","CEO","ASSISTANT"),
SSN=c("123-34-2346","123-44-779","556-24-433","123-98-987","679-77-576"))
print("Details of the employees:")
print(Employees)
#Slip no:10
Q1. Write a R program to find the maximum and the minimum value of a given vector.
Answer:
nums = c(10, 20, 30, 40, 50, 60)
print('Original vector:')
print(nums)
print(paste("Maximum value of the said vector:",max(nums)))
print(paste("Minimum value of the said vector:",min(nums)))
#Slip no:11
Q1. Write a R program to find all elements of a given list that are not in another given list.
AB= list("x", "y", "z")
= list("X", "Y", "Z", "x", "y", "z")
Answer:
l1 = list("x", "y", "z")
l2 = list("X", "Y", "Z", "x", "y", "z")
print("Original lists:")
print(l1)
print(l2)
print("All elements of l2 that are not in l1:")
setdiff(l2, l1)
#Slip no: 13
Q1. Draw a pie chart using R programming for the following data distribution:
Answer:
Create data for the graph.
digits <- c(7,2,6,3,4,8)
Frequency <- c(1,2,3,4,5,6)
Plot the chart.
pie(digits, Frequency)
#Slip no: 14
Q1. Write a script in R to create a list of employees (name) and perform the following:
a. Display names of employees in the list.
b. Add an employee at the end of the list
c. Remove the third element of the list
Answer:
list_data <- list("Ram Sharma","Sham Varma","Raj Jadhav", "Ved Sharma")
print(list_data)
new_Emp <-"Kavya Anjali"
list_data <-append(list_data,new_Emp)
print(list_data)
list_data[3] <- NULL
print(list_data)
#Slip no: 20
Q1. Write a R program to create a data frame from four given vectors.
Answer:
name = c('Aarya', 'Riya', 'Shweta', 'Anjali', 'Geeta', 'Mayuri', 'Kirti', 'Akansha', 'Kavita', 'Jagruti')
score = c(12.5, 9, 16.5, 12, 9, 20, 14.5, 13.5, 8, 19)
attempts = c(1, 3, 2, 3, 2, 3, 1, 1, 2, 1)
qualify = c('yes', 'no', 'yes', 'no', 'no', 'yes', 'yes', 'no', 'no', 'yes')
print("Original data frame:")
print(name)
Create four vectors
name <- c("Alice", "Bob", "Charlie", "David")
age <- c(25, 30, 22, 35)
city <- c("New York", "San Francisco", "Los Angeles", "Chicago")
salary <- c(50000, 60000, 45000, 70000)
Create a data frame
my_data_frame <- data.frame(name, age, city, salary)
Display the result
print(my_data_frame)