You are on page 1of 18

LAB - 7

Name: Sam Melvin M Course: FDA

Reg no: 19MIS1106 Course Code: CSE3505

----------------------------------------------

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)

#4)Swapping two numbers using function


Code:-
x=readline(prompt="Enter a Number: ")
y=readline(prompt="Enter a Number: ")
f<-function(x,y){
print(paste("Before Swapping: X = ",x," and Y = ",y))
temp=x
x=y
y=temp
print(paste("After Swapping: X = ",x," and Y = ",y))
}
x=as.integer(x)
y=as.integer(y)
f(x,y)

#5) Get the three values from the user and do the sum,mul,sub
and mod.
Code:-

a=readline(prompt = "Enter the number: ")


b=readline(prompt = "Enter the number: ")
c=readline(prompt = "Enter the number: ")
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)
#6)Write the function to print the reverse of seq 1:20

n=readline(prompt = "Enter the number: ")


revers<-function(n){
print(rev(1:n))
}
n=as.numeric(n)
revers(n)

#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)

#9)Write a function to print the elements in A that are not in B


a = c(1, 3, 8, 29, 9, 71, 90)
b = c(17, 8, 6, 90)
print("vector a is")
print("vector b is")
print("Elements of vector a that are not in vector b are:")
ans = setdiff(a, b)
print(ans)

#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

6. Display the names and total scores of all participants.


Total<-rowSums(dat[,2:8])
dat<-cbind(dat,Total)
print(dat[,c(1,9)])
7. Make the column “name” as the row index of the data frame.
rownames(dat) <- dat$Name
8. Display the names of the students participated in event3.

9. Obtain the names whose total score is above its average.

OUTPUT
1

2-6
7

9
PART-C
#MASS Package

1. Install the package MASS.


install.packages("MASS")
2. Import the package MASS.
library("MASS")
View(survey)
3. Display the structure of the data survey.
str(survey) #structure of the survey
df<-data.frame(survey)
df
4. Check the class and type of the data set survey in MASS.
#Class and Datatype
class(survey)
typeof(survey)
5. Get the number of rows and columns of the survey data frame.
nrow(survey)
ncol(survey)
6. Get the dimension of the survey data frame.
dim(survey)
7. Provide the statistical summary of the data frame.
summary(survey)
8. Display the column names of the survey data frame
colnames(survey)
9. Retrieve the top 3 rows from the data frame.
head(survey,3)
10. Extract the bottom 2 rows from the data frame.
tail(survey,2)

OUTPUT:-

You might also like