Experiment 6
Experiment 6
Apparatus:
Sr. No. System requirements Range / Value
1 Processors Intel® Core™ i5 processor 4300M
at 2.60 GHz or 2.59 GHz
2 RAM 4 GB (min)
3 Operating systems Windows 10, MacOS, Linux
4 Disk space 1 GB (min)
5 Software R-4.0.4 for Windows (32/64 bit),
RStudio Desktop 1.4.1103
Theory:
R has many in-built functions which can be directly called in the program without defining them
first. The given link: https://cran.r-project.org/doc/contrib/Short-refcard.pdf has a list of standard
functions available in R. However, one can also create and use our own functions referred as user
defined functions. An R function is created by using the keyword function. The basic syntax of
an R function definition is:
Procedure:
1. Define a vector of integers
2. Use standard functions prod(), sum(), mean(), sd() and median() on the defined vector
and print the results
3. Create a user defined function that will calculate the product, sum, mean, standard
deviation and median using mathematical formula. Call the function by passing the
defined input vector.
4. Compare the results obtained with the results obtained using standard functions. Draw
your conclusions.
Source Code:
Using Standard Functions:
c=seq(A,B,2)
print(c)
p=prod(c)
q=sum(c)
r=mean(c)
s=sd(c)
t)
Inbuilt Functions:
c=seq(A,B,2)
print(c)
m=1
for(p in 1:length(c))
m=m*c[p]
//Addition
n=0
for(q in 1:length(c))
{
n=n+c[q]
//Mean
j=0
for(u in 1:length(c))
j=(j+c[u])
x=j/length(c)
//Standard Deviation
k=0
for(w in 1:length(c))
k=k+(c[w]-x)^2
l=sqrt(k/length(c)-1)
//Median
if(length(c)%%2!=0)
e=c[(length(c)+1)/2]
}else
{
e=(c[(length(c)/2)]+c[(length(c)/2)+1])/2
//Variance
z=0
for(h in 1:length(c))
z=z+(c[h]-x)^2
v=l^2
my=function(){
c=seq(A,B,2)
print(c)
m=1
for(p in 1:length(c))
m=m*c[p]
for(q in 1:length(c))
n=n+c[q]
j=0
for(u in 1:length(c))
j=(j+c[u])
x=j/length(c)
k=0
for(w in 1:length(c))
k=k+(c[w]-x)^2
l=sqrt(k/length(c)-1)
if(length(c)%%2!=0)
e=c[(length(c)+1)/2]
}else
{
e=(c[(length(c)/2)]+c[(length(c)/2)+1])/2
z=0
for(h in 1:length(c))
z=z+(c[h]-x)^2
v=l^2
my()
Conclusion:
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
Date of
Performance: _________
Date of
Correction:___________
Weight 20 20 100
Roll No:_________
Marks: ______/100
Signature of Faculty:
ii. Create user defined functions for product, sum, mean, standard
√
deviation and median using formula