R functions are used to provide users with programmed procedures. There are many convenient functions which come with R. For example, you have used c(), rbind(), dim(), read.table(). You can learn how to use functions by help(``functionName''). If you don't know the function name, use help.search(``keywords'').
> help("read.table") > help.search("ridge regression")
standardize <- function(x) { # Inputs: a vector x # Outputs: the standardized version of x m<-mean(x) std<-sqrt(var(x)) result<-(x - m)/std return(result) }
You can use this function in the same way you use the build-in functions.
> a <- c(1,4,6,10, 12) > astd <- standardize(a)
So functions make it easier to do the same analysis many times.
To see the commands which make up the function, just type the name of functions WITHOUT parentheses.
> standardize
Typing in the functions each time is tedious. Instead, you can type the useful custom functions in a plain text file. You keep accumulating your small useful programs in this file and analysis become quicker. You can read in all of your functions next time you need it by source(). In this way, you can automate all of the analysis, and distribute your analysis programs (functions) for other people to do the same analysis.
> source("myCmd.r") # read in the text file, and execute the commands
The R commands, for() or while(), are used for iteration. Here is an example of using for() inside a function:
Watterson's coefficient is
.
wattersonCoef <- function(m) { tempSum <- 0 for (i in 1:(m-1)) { tempSum <- tempSum + 1 / i } return(tempSum) } ### same thing using while() wattersonCoefAlt <- function (m) { tempSum <- 0 cntr <- 1 while (cntr <= m-1) { tempSum <- tempSum + 1 / cntr cntr <- cntr + 1 } return(tempSum) }
absVal <- function (val) { if (length(val) > 1) { # Note that val should be a single value, not a vector return(NA) } else if (val < 0) { return (- val) } else { return (val) } }
# returns 100 rand numbers following binominal distn of 40 trials w/ p = 0.2 > binom <- rbinom(n = 100, size = 40, prob = 0.2) > hist(binom) > mean(binom) > var(binom) # 40 * 0.2 * (1-0.2) ## normal distribution > norm <- rnorm(500, mean=0, sd=1) ## poisson distribution > poi <- rpois(n=1000, lambda=4) > hist(poi) > mean(poi) > var(poi) # mean = var = lambda ## geometric distribution > geo <- rgeom(n=1000, prob=0.1)