# Section 7: User-defined functions in R

User-defined functions can also be created and saved in R. As a simple example, the following code creates a user-defined function to calculate a 95% confidence interval for a proportion. The function name is 'CIp', and the input for the function is p (the sample proportion) and n (the sample size). The '+'s at the beginning of lines were typed by R and indicate a continuation of the previous line/calculation. The '{ }'s in the function specification indicate individual calculations or function calls within the function. 'cresult' is a column vector containing lower (p.lower) and upper (p.upper) confidence limits, and the 'return( )' function indicates which of the objects created in the function are to be printed when the function is called.

> CIp <- function(p,n) {

+ {p.lower <- p-1.96*sqrt(p*(1-p)/n)}

+ {p.upper <- p+1.96*sqrt(p*(1-p)/n)}

+ {cresult <- c(p.lower,p.upper)}

+ return(cresult)

+ }

> CIp(.30,100)

 0.2101815 0.3898185

> CIp(.3,1000)

 0.2715969 0.3284031

The following creates a function to calculate two-tailed p-values from a t-statistic. The cat( ) function specifies the print out. Unlike the return( ) function (I think), cat( ) allows text labels to be included in quotes and more than one object to be printed on a line. The '\n' in the cat( ) function inserts a line return after printing the label and p-value, and multiple line returns could be specified in a cat( ) statement.

> pvalue.t <- function(t,df) {

+ pval <- 2*(1-pt(abs(t),df))

+ cat('Two-tailed p-value',pval,'\n')

+ }

> pvalue.t(2.33,200)

Two-tailed p-value 0.02080349

> pvalue.t(-1.55,45)

Two-tailed p-value 0.1281466