myfunction<-function(arg1,arg2,...){ statements return (object) }
例1
function1<-function(x,y){plot(x,y) + return(x+y)}
x<-rnorm(10)
x
y<-rnorm(10,2,3)
function1(x,y)
例2 嵌套条件
> function2<-function(x,npar=TRUE,print=TRUE){ + if(!npar){ + center<-mean(x);spread<-sd(x) + }else{ + center<-median(x);spread<-mad(x) + + } + if (print & !npar){ + cat("Mean",center,"\n","SD=",spread,"\n") + }else if(print & npar){ + cat("Median=",center,"\n","MAD=",spread,"\n") + } + result<-list(center=center,spread=spread) + return(result) + } x<-rnorm(10,0,1) function2(x)
条件