R Skill Lab Programs
R Skill Lab Programs
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:
LIST OF PROGRAMS:
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:
Program:
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:
Program:
P2=function()
a=readline("Enter something:")
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:
Program:
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("\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
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:
Program:
P4=function()
n=as.integer(readline("Enter n value:"))
if(n%%2==0)
else
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:
Program:
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)
else
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:
Program:
P6=function()
n=as.integer(readline("Enter a number:"))
if(n>=0)
}else
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:
Program:
P7=function()
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:
Program:
P8=function()
while(T)
if(n>0)
break
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:
Program:
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")
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
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:
Program:
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:
Program:
P11=function()
x=0
for(i in 1:n)
x[i]=as.numeric(readline())
cat(x)
if(y%in%x)
else
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
Program:
#Program to create a Matrix and access rows and columns using functions colnames() and
rownames()
P12=function()
x=0
for(i in 1:(r*c))
x[i]=as.integer(readline())
matrixa=matrix(x,nrow=r,ncol=c,byrow=T)
rn=c()
for(i in 1:r)
rn[i]=readline()
rownames(matrixa)=rn
cn=c()
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)
if((rname%in%rn)&&(cname%in%cn))
print(matrixa[rname,cname])
else
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:
Program:
P13=function()
mata=rbind()
x=0
for(i in 1:m)
for(j in 1:n)
x[j]=as.integer(readline())
print(mata)
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
matb=cbind()
x=0
for(i in 1:n)
for(j in 1:m)
x[j]=as.integer(readline())
matb=cbind(matb,assign(paste0("col_",i),x))
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:
Program:
P14=function()
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
Program
# 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)
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
Program
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
Program
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)
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
Program
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
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
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
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"
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
Program
myfun=function(obj)
{
UseMethod("myfun")
}
# 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
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")
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
Program
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
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)
48
n-gl.com