Professional Documents
Culture Documents
----------------------------------------------
PART-A
#10 Questions given in the class
#1)Create a function to print squares in sequence
Code:-
n=readline(prompt="Enter a Number: ")
new.function <- function(a)
{
for(i in 1:a)
{
b <- i^2
print(b)
}
}
new.function(n)
#2)write a c program to print the following series 1 3 5 7 9 11 .
Code:-
numbers <- function(n) {
if (n >= 2) {
j=1
for (i in 1:n) {
print(j)
j=j+2
}
}
else
{
stop("Input number should be at least 2.")
}
}
numbers(6)
#3)function to print the area of the Rectangle
Code:-
l=readline(prompt="Enter a Number: ")
b=readline(prompt="Enter a Number: ")
f<-function(l,b){
return(l*b)
}
l=as.numeric(l)
b=as.numeric(b)
f(l,b)
#5) Get the three values from the user and do the sum,mul,sub
and mod.
Code:-
#7)Create the three values defult and do the sum ,diff,mul and
mod.
a=5
b=6
c=7
sum<-function(a,b,c){
print(a+b+c)
}
diff<-function(a,b,c){
print(a-b-c)
}
mul<-function(a,b,c){
print(a*b*c)
}
a=as.numeric(a)
b=as.numeric(b)
c=as.numeric(c)
mul(a,b,c)
diff(a,b,c)
sum(a,b,c)
#8) Write a function to perform the matrix multiplication.
a<-matrix(c(2,4,6,7,8,6,4,8,4),ncol = 3)
b<-matrix(c(9,5,8,3,7,1,6,4,2),nrow = 3)
a
b
matmul<-function(a,b){
print(a%*%b)
}
matmul(a,b)
#10)Get three vector from the user and perform the function
AUB,AnB,A-B,AU(BnC),An(BUC)
A<- scan()
B<- scan()
C<- scan()
A
B
C
union(A,B)
intersect(A,B)
intersect(A,union(B,c))
union(A,intersect(B,C))
setdiff(A,B)
PART-B
Event.csv dataset
1. Read the content of ‘Events.csv’ in a data frame and view it.
dat <- read.csv("book1.csv")
View(dat)
2. Access the scores of participants in event2 using the column name.
print(dat$Event2)
3. Use index number to retrieve the same data.
print(dat$Event2[1:20])
4. Extract the score of third participant in event3.
print(dat$Event3[3])
5. Extract the scores of the first and second participant in all the events.
da<-dat[1:2,]
da
OUTPUT
1
2-6
7
9
PART-C
#MASS Package
OUTPUT:-