0% found this document useful (0 votes)
10 views10 pages

Experiment 6

Uploaded by

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

Experiment 6

Uploaded by

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

Experiment 6

Comparison between standard and user defined functions


Aim: To compare between standard functions and user defined functions

Practical Learning Objectives:

The objective of this practical is to

i. Use standard functions viz. prod(), sum(), mean(), sd(), median()


ii. Create user defined functions for product, sum, mean, standard deviation and median
using formula
iii. Compare if the values obtained are same

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:

The different parts of a function are:


• Function Name: This is the actual name of the function. It is stored in R environment as an
object with this name.
• Arguments: An argument is a placeholder. When a function is invoked, you pass a value to
the argument. Arguments are optional; that is, a function may contain no arguments. Also
arguments can have default values.
• Function Body: The function body contains a collection of statements that defines what the
function does.

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:

A=readline("Enter the initial value:")

B=readline("Enter the ending value:")

c=seq(A,B,2)

print(c)

p=prod(c)

cat("The product of the input vector of numbers is",p)

q=sum(c)

cat("The sum of the input vector of numbers is",q)

r=mean(c)

cat("The mean of the input vector of numbers is",r)

s=sd(c)

cat("The standard deviation of the input vector of numbers is",s)


t=median(c)

cat("The median of the input vector of numbers is",

t)

Inbuilt Functions:

A=readline("Enter the initial value:")

B=readline("Enter the ending value:")

c=seq(A,B,2)

print(c)

m=1

for(p in 1:length(c))

m=m*c[p]

cat("The product is:",m)

//Addition

n=0

for(q in 1:length(c))

{
n=n+c[q]

cat("\n The Addition is:",n)

//Mean

j=0

for(u in 1:length(c))

j=(j+c[u])

x=j/length(c)

cat("\n The mean is",x)

//Standard Deviation

k=0

for(w in 1:length(c))

k=k+(c[w]-x)^2

l=sqrt(k/length(c)-1)

cat("\n The Standard deviation is",l,"\n")

//Median

if(length(c)%%2!=0)

e=c[(length(c)+1)/2]

}else
{

e=(c[(length(c)/2)]+c[(length(c)/2)+1])/2

cat("The median is",e,"\n")

//Variance

z=0

for(h in 1:length(c))

z=z+(c[h]-x)^2

v=l^2

cat("\n The Variance is",v,"\n")


Call the functions based on user input:

my=function(){

A=readline("Enter the initial value:")

B=readline("Enter the ending value:")

c=seq(A,B,2)

print(c)

m=1

for(p in 1:length(c))

m=m*c[p]

cat("The product is:",m)


n=0

for(q in 1:length(c))

n=n+c[q]

cat("\n The Addition is:",n)

j=0

for(u in 1:length(c))

j=(j+c[u])

x=j/length(c)

cat("\n The mean is",x)

k=0

for(w in 1:length(c))

k=k+(c[w]-x)^2

l=sqrt(k/length(c)-1)

cat("\n The Standard deviation is",l,"\n")

if(length(c)%%2!=0)

e=c[(length(c)+1)/2]

}else
{

e=(c[(length(c)/2)]+c[(length(c)/2)+1])/2

cat("The median is",e,"\n")

z=0

for(h in 1:length(c))

z=z+(c[h]-x)^2

v=l^2

cat("\n The Variance is",v,"\n")

my()
Conclusion:

__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________
__________________________________________________________________

Outco PLO PLO PLO Performan Attendan Total E&TC DEPARTMENT-


me 1 2 3 ce ce Score TCET

Date of
Performance: _________

Date of
Correction:___________
Weight 20 20 100
Roll No:_________

Marks: ______/100

Signature of Faculty:

Practical Learning Outcomes:


After performing the practical, the learner is able to: Marked

i. Use standard functions viz. prod(), sum(), mean(), sd(), √


median()

ii. Create user defined functions for product, sum, mean, standard

deviation and median using formula

iii. Compare if the values obtained are same


You might also like