0% found this document useful (0 votes)
23 views

R Skill Lab Programs

Uploaded by

abhiramragu51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

R Skill Lab Programs

Uploaded by

abhiramragu51
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

R-PROGRAMMING LAB (Skill Course)

AR20- B.Tech(Common to CSE and DS, AI & ML Specializations)


II-B.Tech., II-Semester
L T P C
1 0 2 2

Course Code: 20CS4203 Internal Marks: 15


External Marks: 35

Course Objectives:
The objectives of R-PROGRAMMING are,
 Understanding and being able to use basic programming concepts
 Automated at a analysis
 Working collaboratively and openly on code
 Knowing how to generate dynamic documents
 Being able to use a continuous test-driven development approach

Course Outcomes:

 Be able to use R to solve statistical problems


 Be able to implement and describe MonteCarlothe technology
 Be able to minimize and maximize functions using R

LIST OF PROGRAMS:

1. Write an R-Program to print Hello World


2. Write an R-Program to take input from user.
3. Write an R-Program to demonstrate working with operators (Arithmetic, Relational,
Logical, Assignment operators).
4. Write an R Program to Check if a Number is Odd or Even
5. Write an R Program to Check if the given Number is a Prime Number
6. Write an R Program to Find the Factorial of a Number
7. Write an R Program to Find the Factors of a Number
8. Write an R Program to Find the Fibonacci sequence Using Recursive Function
9. Write an R Program to Make a Simple Calculator
10. Write an R Program to Find L.C.M of two numbers
11. Write an R Program to o create a Vector and to access elements in a Vector
12. Write an R Program to create a Matrix and access rows and columns using functions
colnames()and rownames().

13. Write an R Program to create a Matrix using cbind() and rbind() functions.
14. Write an R Program to create a Matrix from a Vector using dim() function.
15. Write an R Program to create a List and modify its components.
16. Write an R Program to create a Data Frame.
17. Write an R Program to access a Data Frame like a List.
18. Write an R Program to access a Data Frame like a Matrix.
19. Write an R Program to create a Factor.
20. Write an R Program to Access and Modify Components of a Factor.
21. Write an R Program to create an S3 Class and S3Objects.
22. Write an R Program to write a own generic function in S3 Class.
23. Write an R Program to create an S4 Class and S4 Objects.
24. Write an R Program to write a own generic function in S4 Class.
25. Write an R Program to create Reference Class and modify its Methods

TEXTBOOKS:
1. SandipRakshit, R Programming for Beginners, McGraw Hill Education (India), 2017,
ISBN : 978-93-5260-455-5.
2. R Programming for Data Science by Roger D.Peng
3. The Art of R Programming by Prashanth singh,VivekMourya,CengageLearningIndia

REFERENCES
1. Andrie de Vries, JorisMeys, R for Dummies A Wiley Brand, 2nd Edition, John Wiley
and Sons, Inc, 2015, ISBN: 978-1-119-05580-8
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise-1

Question:

Write an R-Program to print Hello World

Program:

#Program to print Hello! world

P1=function()

print("Hello! world")

P1()

Output:

3
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise-2:

Question:

Write an R-Program to take input from user.

Program:

#Program to take input from user and print it

P2=function()

a=readline("Enter something:")

cat("Entered input is:",a)

P2()

Output:

4
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise-3:

Question:

Write an R-Program to demonstrate working with operators (Arithmetic, Relational,

Logical, Assignment operators).

Program:

# R-Program to demonstrate working with operators (Arithmetic, Relational, Logical,


Assignment operators).

P3=function()

a=as.numeric(readline("Enter a value:"))

b=as.numeric(readline("Enter a value:"))

c=as.numeric(readline("Enter a value:"))

cat("\nArthimetic operators")

cat("\nsum=",a+b,"\nDifference=",a-b,"\nProduct=",a*b,"\nDivisio:",a/b,
"\nRemainder:",a%%b)

cat("\nRelational Operators:")

cat("a>b:",a>b,"\n")

cat("a<c:",a<c,"\n")

cat("Logical operators:")

cat("\nc(0,1)&c(1,1):",c(0,1)&c(1,1))#compares all elements

cat("\nc(0,1)&&c(1,1):",c(0,1)&&c(1,1))#compares first element only

cat("\nc(0,1)|c(1,1):",c(0,1)|c(1,1))#compares all elements

cat("\nc(0,1)||c(1,1):",c(0,1)||c(1,1))#compares first element only

cat("\n!c(0,1):",!c(0,1))#complements

cat("\nAssignment operator:")

5
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

d=a#a value assigned to d

cat("a value is",a,"\nd value is",d)

P3()

Output:

6
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise-4:

Question:

Write an R Program to Check if a Number is Odd or Even

Program:

#Program to check the given number is even or odd

P4=function()

n=as.integer(readline("Enter n value:"))

if(n%%2==0)

cat(n,"is an even number")

else

cat(n,"is not an odd number")

P4()

Output:

7
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

8
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise-5:

Question:

Write an R Program to Check if the given Number is a Prime Number

Program:

#Program to check the given number is prime or not

P5=function()

n=as.numeric(readline("Enter a number:"))

if(n>=0)

if(n==2)

return("Prime number")

for(i in 2:sqrt(n))

if(n%%i==0)

return("Given number is not a prime number")

print("Given number is a prime number")

else

print("Please enter a non-negetive number")

prime()

9
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

P5()

Output:

10
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise-6

Question:

Write an R Program to Find the Factorial of a Number

Program:

#Program to find factorial of a number

P6=function()

n=as.integer(readline("Enter a number:"))

if(n>=0)

cat("Factorial of ",n," is",fact(n))

cat("\nFactorial of ",n," by recursion is ",factr(n))

}else

print("Please enter a non-negetive number")

P6()

fact=function(n)

x=1

if(n==0)

Return(1)

}else

11
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

for(i in 1:n)

x=x*i

return(x)

factr=function(n)

if(n==0 || n==1)

return(1)

else

return(n*factr(n-1))

P6()

Output:

12
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise-7

Question:

Write an R Program to Find the Factors of a Number

Program:

#Prgoram to find the factors of the given number

P7=function()

n=as.integer(readline("Enter a positive number:"))

if(n>0)

x=''

j=1

for(i in 1:n)

if((n%%i)==0)

x[j]=as.numeric(i)

j=j+1

cat("Factors of",n,"are:",as.numeric(x))

}else

P7()

P7()

13
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Output:

14
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise-8

Question:

Write an R Program to Find the Fibonacci sequence Using Recursive Function

Program:

#Program to find Fibonacci series using recursion

P8=function()

while(T)

n=as.integer(readline("Enter a positive number number:"))

if(n>0)

break

cat("Fibonacci series is:\n")

for(i in 0:(n-1))

cat(fib1(i)," ")

fib1=function(i)

if(i<2)

return(i)

else

return(fib1(i-1)+fib1(i-2))

P8()

15
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Output:

16
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Excercise-9

Question:

Write an R Program to Make a Simple Calculator

Program:

#Program for simple calculator

P9=function()

a=as.numeric(readline("Enter a value:"))

b=as.numeric(readline("Enter b value:"))

cat("\n","1.Addition","\n","2.Substraction","\n","3.Multiplication","\n",

"4.Division")

cat("Enter your choice(1-4)")

ch=as.numeric(readline())

if(ch==1)

cat("sum=",a+b)

else if(ch==2)

cat("Difference=",a-b)

else if(ch==3)

cat("Product=",a*b)

else if(ch==4)

if(b!=0)

cat("Division:",a/b)

else

cat("Division is not possible")

else

17
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

cat("Wrong choice")

P9()

Output:

18
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise-10

Question:

Write an R Program to Find L.C.M of two numbers

Program:

#Program to find lcm of 2 numbers

P10=function()

a=as.integer(readline("Enter a value:"))

b=as.integer(readline("Enter b value:"))

max=max(a,b)

min=min(a,b)

for(i in 1:min)

l=max*i

if((l%%min==0))

cat("LCM of",a,",",b,"is",l)

break

P10()

19
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Output:

20
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise-11

Question:

Write an R Program to o create a Vector and to access elements in a Vector

Program:

#Program to create a Vector and to access elements.

P11=function()

x=0

n=as.integer(readline("Enter the size of the vector:"))

for(i in 1:n)

x[i]=as.numeric(readline())

cat("The vector is \n")

cat(x)

p=as.integer(readline("\nEnter the position of the element the vector:"))

cat("\nElement in the vector at",p,"position is",x[p])

y=as.integer(readline("\nEnter the element to search in the vector:"))

if(y%in%x)

cat("Element found at ",which(x==y))

else

cat("Element not found")

P11()

21
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Output:

22
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise-12

Question:

Write an R Program to create a Matrix and access rows and columns using functions

colnames() and rownames().

Program:

#Program to create a Matrix and access rows and columns using functions colnames() and
rownames()

P12=function()

r=as.integer(readline("Enter the number of rows of the matrix:"))

c=as.integer(readline("Enter the number of columns of the matrix:"))

cat("Enter the elements of the matrix:\n")

x=0

for(i in 1:(r*c))

x[i]=as.integer(readline())

matrixa=matrix(x,nrow=r,ncol=c,byrow=T)

rn=c()

cat("Enter the row names of the matrix:\n")

for(i in 1:r)

rn[i]=readline()

rownames(matrixa)=rn

cn=c()

cat("Enter the column names of the matrix:\n")

for(i in 1:c)

cn[i]=readline()

colnames(matrixa)=cn

23
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

cat("Matrix is:\n")

print(matrixa)

rname=readline("Enter the row name of the element to display:")

cname=readline("Enter the column name of the element to display:")

if((rname%in%rn)&&(cname%in%cn))

print(matrixa[rname,cname])

else

cat("Invalid row/column name")

P12()

Output:

24
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise-13

Question:

Write an R Program to create a Matrix using cbind() and rbind() functions

Program:

#Program to create matrix using cbind and rbind commands

P13=function()

#creating vector using rbind() function

m=as.integer(readline("Enter the number of rows:"))

n=as.integer(readline("Enter the number of columns:"))

mata=rbind()

x=0

for(i in 1:m)

cat("Enter the elemnts of row:",i,"\n")

for(j in 1:n)

x[j]=as.integer(readline())

mata=rbind(mata,assign(paste0("row_",i),x))#do display row names properly

cat("Matrix created by using \"rbind()\" is:\n")

print(mata)

#creating vector using cbind() function

m=as.integer(readline("Enter the number of rows:"))

25
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

n=as.integer(readline("Enter the number of columns:"))

matb=cbind()

x=0

for(i in 1:n)

cat("Enter the elemnts of column:",i,"\n")

for(j in 1:m)

x[j]=as.integer(readline())

matb=cbind(matb,assign(paste0("col_",i),x))

#do display column names properly

cat("Matrix created by using \"cbind()\" is:\n")

print(matb)

P13()

26
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Output:

27
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise-14

Question:

Write an R Program to create a Matrix from a Vector using dim() function.

Program:

#Program to create a Matrix from a Vector using dim() function.

P14=function()

r=as.integer(readline("Enter the number of rows of the matrix:"))

c=as.integer(readline("Enter the number of columns of the matrix:"))

cat("Enter the elements of the matrix:\n")

x=0

for(i in 1:(r*c))

x[i]=as.integer(readline())

dim(x)=c(r,c)

cat("Matrix is:\n")

print(x)

P14()

28
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Output:

29
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise 15

Question

Write a R program to create a list and modify its components.


Create a list containing roll number-170981A0201, student name-Md.Ahmed, gender-Male
and number of backlogs-2 for a student and do the following.
(i) Give names to the components of the list
(ii) Add a component with name passport_no. at the end of the list
( iii) Remove the third component
( iv) update the number of backlogs to 1

Program

stu_list <- function()


{
student=list("170981A0201","Md.Ahmed","Male",2)

# Print the list


cat("The student details are \n")
print(student)

# (i) Give names to the components of the list


names(student)=c("Roll_no.","Name","Gender","Backlogs")

# Print the list with names


cat("The list with component names \n")
print(student)

# ii) Add a component with name passport_no. at the end of the list
student$passport_no.="M7802031"
cat("The list with passport number \n")
print(student)

# iii) Remove the third component


student$Gender=NULL
cat("The list after removing gender\n")
print(student)

# iv) update the number of backlogs to 1


student$Backlogs=1
cat("The list with updated backlogs is \n")
print(student)
}

30
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Output:

31
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise 16

Question

Write a R program to create a data frame.


Create a data frame of five students with five fields namely Roll number, name, gender, blood group
and grade. Print the structure and summary of the data frame.

Program

st1_df <- function()


{

roll_number=c("200981A0101","200981A0102","200981A0103","200981A0104","200981A0105")
name=c("Ahmed","John","Rama","Begum","Rani")
gender=c("M","M","M","F","F")
blood_group=factor(c("A+","B-","o","A-","0"))
grade=c(8,8.5,7.5,9,8.5)
stud_df=data.frame(roll_number,name,gender,blood_group,grade)
cat("\n The data frame is\n ")
print(stud_df)
cat("\n The structure of the data frame is \n")
print(str(stud_df))
cat("\n The Statistical summary of the given data is \n")
print(summary(stud_df))
}

Output

32
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise 17

Question

Write a R program to access a data frame like a list.


Create a data frame of five students with five fields
namely Roll number, name, gender, blood group and grade.
write a code
1) to extract specific column namely Grade from a data frame like a list.
2) to extract specific column namely roll_number from a data frame like a list.
3) to extract specific column namely name from a data frame like a list.
4) to drop column (Gender) by name from a given data frame.

Program

st2_df <- function()


{

roll_number=c("200981A0101","200981A0102","200981A0103","200981A0104","200981A0105")
name=c("Ahmed","John","Rama","Begum","Rani")
gender=c("M","M","M","F","F")
blood=c("A+","B-","o","A-","0")
blood_group=factor(blood)
grade=c(8,8.5,7.5,9,8.5)
stud_df=data.frame(roll_number,name,gender,blood_group,grade)
cat("\n The data frame is\n ")
print(stud_df)

cat("\n Extracting the specific column Grade\n")


print(stud_df$grade)
cat("\n Extracting the specific column roll_numbter\n")
print(stud_df$roll_number)
cat("\n Extracting the specific column name\n")
print(stud_df$name)
cat("\n Droping column (Gender) by name from a given data frame \n")
stud_df$gender=NULL
print(stud_df)
}

33
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Output

34
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise 18

Question

Write a R program to access a data frame like a matrix.


Create a data frame of five students with five fields namely Roll number, name, gender, blood group
and grade.
Write a code
1) to extract first two rows from a given data frame.
2) to add two students at the end of the data frame.
3) to extract 3rd row and 5th row with column names "name" and "grade".
4) to drop row(s) (3rd and 4th rows) by number from a given data frame.

Program

st3_df <- function()


{
roll_number=c("200981A0101","200981A0102","200981A0103","200981A0104","200981A0105")
name=c("Ahmed","John","Rama","Begum","Rani")
gender=c("M","M","M","F","F")
blood=c("A+","B-","o","A-","0")
blood_group=factor(blood)
grade=c(8,8.5,7.5,9,8.5)
stud_df=data.frame(roll_number,name,gender,blood_group,grade)
cat("\n The data frame is\n ")
print(stud_df)
cat("\n Extracting first two rows from a given data frame\n")
print(stud_df[1:2,])
cat("\n Extracting the records whose grade is greater than 8\n")
print(subset(stud_df,grade >8))
cat("\n Adding two more students at the end of the data frame\n")
s_df=data.frame(roll_number=c("200981A0107","200981A0108"),name=c("Ravi","Alekya),gender=
c("M","F"),blood_group=factor(c("A+","B-")),grade=c(8,9))
stud_df=rbind(stud_df,s_df)
print(rbind(stud_df,s_df))
cat("\n Extracting 3rd row and 5th row with column names name and grade\n")
print(stud_df[c(3,5),c(2,5)])
cat("\nDroping row(s) (3rd and 4th rows) by number from a given data frame\n")
stud_df=stud_df[-3:-4,]
print(stud_df)
}

35
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Output

36
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise 19

Question

Write a R program to create a factor.


Create a factor for the variable 'marital status' of the employees in a company and print the factor
variable and structure.
The marital status of 10 employees: single, married, divorced, single, married, married,
single, married, married, single

Program

fac<-function()
{
status=c("single","married","divorsed","single","married","married",
"single","married","married","single")
m_status=factor(status)
print("The variable status as a factor variable is ")
print(m_status)
cat("\n","The structure of the factor variable is ","\n")
print(str(m_status))
}

Output

37
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise 20

Question

Write a R program to access and modify components of a factor.


Create a factor for the variable 'marital status'
of the employees in a company and access the components of the factor.
Also modify the components of the factor.
Print the 3rd component of the factor variable.
Print the 1sr and 4th components of the factor variable.
Modify the 3rd component as married.
Print all the levels of the factor variable.
Add a new level "widowed" to the factor levels.
The marital status of 10 employees: single, married, divorced, single, married, married,
single, married, married, single

Program

fac1<-function()
{
status=c("single","married","divorced","single","married","married",
"single","married","married","single")
m_status=factor(status)
print("The variable status as a factor variable is ")
print(m_status)
cat("\n","The structure of the factor variable is ","\n")
print(str(m_status))
cat("\n","The 3rd component of the factor variable is ","\n")
print(m_status[3])
cat("\n","The 1st and 4th components of the factor variable is ","\n")
print(m_status[c(1,4)])
cat("\n","Modifying the factor 3rd component as married ","\n")
m_status[3]="married"
print(m_status)
cat("\n","The levels of the factor are ","\n")
print(levels(m_status))
cat("\n","Adding a new level widowed to the factor levels","\n")
levels(m_status)=c(levels(m_status),"widowed")
cat("\n","The updated levels are ","\n")
print(levels(m_status))
}

38
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Output

39
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise 21

Question

Write a R program to create an S3 class and S3 objects

Program

s3_program1<-function()
{
s3_cls=list(name="Ahmad",rollno=786,std="10th")
class(s3_cls)="student"
s3_cls
cat("\n","Type of s3_cls is ",otype(s3_cls),"\n")

#creating objects

obj1=list(name="Anand",rollno=7,std="9th")
obj2=list(name="Prasanthi",rollno=8,std="7th")
obj3=list(name="John",rollno=6,std="8th")

class(obj1)="student"
class(obj2)="student"
class(obj3)="student"

cat("\n","Object named obj1 is ","\n")


print(obj1)
cat("\n","Object named obj2 is ","\n")
print(obj2)
cat("\n","Object named obj3 is ","\n")
print(obj3)

cat("\n","Type of obj1 is ",otype(obj1),"\n")


cat("\n","Type of obj2 is ",otype(obj2),"\n")
cat("\n","Type of obj3 is ",otype(obj3),"\n")
}

40
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Output:

41
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise 22

Question

Write a R program to write a own generic function in S3 calss.

Program

#writing own generic function

myfun=function(obj)
{
UseMethod("myfun")
}

#creating own method for the generic function "myfun"


myfun.details=function(obj)
{
cat("Student name is ",obj$name,"\n")
cat("Student roll number is ",obj$rno,"\n")
cat("Student standard is ",obj$std,"\n")
}

# Creating an object and accessing the components using the method in the own generic funciton
s3_genric<-function()
{
obj2=list(name="raju",rno=1,std="6th")
cat("The components of obj2 are ","\n")
print(obj2)
print("The components after calling the own method of the generic function named
myfun.details are ")
myfun.details(obj2) # printing the details of the object "obj2"
print("Methods available in the generic funciton named myfun are")
methods(myfun) # Shows the available methods in "myfun"
}

42
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Output

43
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise 23

Question

Write a R program to create an S4 class and S4 objects

Program

s4_program1<-function()
{
# Defining s4 class
setClass("Employee",slots=list(name="character",E_id="numeric",Gender="character"))

# Creating an s4 object
e1=new("Employee",name="Ahmad",E_id=786,Gender="Male")
print("The slots of the object are ")
print(e1)
cat("The type of the object named e1 is ",otype(e1))

#creating objects

obj1=new("Employee",name="Anand",E_id=7,Gender="Male")
obj2=new("Employee",name="Prasanthi",E_id=8,Gender="Male")
obj3=new("Employee",name="John",E_id=6,Gender="Male")

cat("\n","Object named obj1 is ","\n")


print(obj1)
cat("Type of obj1 is ",otype(obj1),"\n")

cat("\n","Object named obj2 is ","\n")


print(obj2)
cat("Type of obj2 is ",otype(obj2),"\n")

cat("\n","Object named obj3 is ","\n")


print(obj3)

cat("Type of obj3 is ",otype(obj3),"\n")


}

44
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Output

45
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise 24

Question

Write a R program to write an own generic function in S4 class.

Program

# Writing own s4 generic function

myfn=function(obj)
{
UseMethod("myfn")
}

setClass("Fruits",slots=list(name="character",colour="character",size="numeric"))

setMethod("myfn",
"Fruits",
function(obj)
{
cat("Name of the fruit is ",obj@name,"\n")
cat("Color of the fruit is ",obj@colour,"\n")
cat("Size of the fruit is ",obj@size,"\n")

})
s4_genric<-function()
{

setClass("Fruits",slots=list(name="character",colour="character",size="numeric"))
F1=new("Fruits",name="mango",colour="yellow",size=5)
cat("The slots in the object named F1 are","\n")
show(F1)
cat("The file type of the funciton named myfn is ","\n",ftype(myfn),"\n")
cat("The methods available in the generic function named myfn are","\n")
showMethods(myfn)
print("The details of the fruit using the generic function is ")
myfn(F1)
}

46
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Output:

47
RAGHU ENGINEERING COLLEGE (Autonomous)
(Approved by AICTE, New Delhi & Permanently Affiliated to JNTUK, Kakinada)
NBA and NAAC ‘A’ grade accredited Institute.
Dakamarri, Bheemili Mandal, Visakhapatnam – 531162, A.P.
Phone: 08922-248001 / 221122/9963981111, www.raghuenggcollege.com

Exercise 25

Question

Write an R program to create a Reference class and modify its methods.

Program

ref_program<-function()
{
# Defining a reference class
setRefClass("Emp",fields =list(name="character",e_id="numeric",gender="character"))

# Creating an object

Emp=setRefClass("Emp",fields =list(name="character",e_id="numeric",gender="character"))

e1=Emp(name="Farhatulla",e_id=78,gender="Male")
print("The data available in the object named e1 is ")
print(e1)

cat("the type of the object is ",otype(e1))


}
Output

48

n-gl.com

You might also like