filmov
tv
How to write functions in R programming? convert code to function
Показать описание
#rprogramming #functions #dataanalysis
Here I have explained how to write functions in R. I have demonstrated functions with or without arguments and with or without default values of the arguments.
I have demonstrated how to convert available code into function with the proper use of arguments. For demonstration purposes, I have used code for the Michelis Menthen plot ( A model for enzyme activity) and Monte Carlo simulation to get an estimate of pi.
## Demo Function
## With no argument
DRY = function(){
print(paste(rep("Do not repeat yourself!",nrep)))
}
DRY()
## with argument
DRY = function(nrep){
print(paste(rep("Do not repeat yourself!",nrep)))
}
DRY(3)
## With the default set for arguments
DRY = function(nrep=3){
print(paste(rep("Do not repeat yourself!",nrep)))
}
DRY()
##mm demo function
mm_demo = function(km=1,vmax=1,s=1:10){
v=vmax*s /(km+s) # the equation
require(ggplot2) # calls library gg-plot2
# plots a complete plot
plot=ggplot(df,aes(s,v,fill=v,color=v))+
geom_line()+
geom_point()+
labs(title="Michelis Menten plot", x="[S]", y= "Vo", subtitle=paste("km=",km,"\t","Vmax=",vmax))+
theme_classic()+
geom_hline(yintercept=vmax,linetype="dashed",color="gray60")+
geom_vline(xintercept=km,linetype="dashed",color="gray60")
return(plot)
}
# testing the function with different values of km, vmax and S
mm_demo(1.5,1.5,s=1:100)
######################################
## Monte Carlo function
mc_pi =function(n=100){
x = runif(n, -1,1)
y= runif(n, -1,1)
x_circ =x[x^2+y^2 lessThan 1] # use lessThan symbol here
y_circ= y[x^2+y^2lessThan1] # use lessThan symbol here
plot(x,y,xlim=c(-1,1),ylim=c(-1.2,1.2))
points(x_circ,y_circ,col="red")
return(4*length(x_circ)/ length(x))
}
mc_pi(10000)
Facebook page:
Mail Id:
youtube playlist:
For bibtex file
for citation style
Facebook page:
Mail Id:
youtube playlist:
Here I have explained how to write functions in R. I have demonstrated functions with or without arguments and with or without default values of the arguments.
I have demonstrated how to convert available code into function with the proper use of arguments. For demonstration purposes, I have used code for the Michelis Menthen plot ( A model for enzyme activity) and Monte Carlo simulation to get an estimate of pi.
## Demo Function
## With no argument
DRY = function(){
print(paste(rep("Do not repeat yourself!",nrep)))
}
DRY()
## with argument
DRY = function(nrep){
print(paste(rep("Do not repeat yourself!",nrep)))
}
DRY(3)
## With the default set for arguments
DRY = function(nrep=3){
print(paste(rep("Do not repeat yourself!",nrep)))
}
DRY()
##mm demo function
mm_demo = function(km=1,vmax=1,s=1:10){
v=vmax*s /(km+s) # the equation
require(ggplot2) # calls library gg-plot2
# plots a complete plot
plot=ggplot(df,aes(s,v,fill=v,color=v))+
geom_line()+
geom_point()+
labs(title="Michelis Menten plot", x="[S]", y= "Vo", subtitle=paste("km=",km,"\t","Vmax=",vmax))+
theme_classic()+
geom_hline(yintercept=vmax,linetype="dashed",color="gray60")+
geom_vline(xintercept=km,linetype="dashed",color="gray60")
return(plot)
}
# testing the function with different values of km, vmax and S
mm_demo(1.5,1.5,s=1:100)
######################################
## Monte Carlo function
mc_pi =function(n=100){
x = runif(n, -1,1)
y= runif(n, -1,1)
x_circ =x[x^2+y^2 lessThan 1] # use lessThan symbol here
y_circ= y[x^2+y^2lessThan1] # use lessThan symbol here
plot(x,y,xlim=c(-1,1),ylim=c(-1.2,1.2))
points(x_circ,y_circ,col="red")
return(4*length(x_circ)/ length(x))
}
mc_pi(10000)
Facebook page:
Mail Id:
youtube playlist:
For bibtex file
for citation style
Facebook page:
Mail Id:
youtube playlist: