R functions are used to provide users with programmed procedures for a number of statistical and non-statistical procedures. Many functions are available in R. All basic functions are part of the BASE package. However, other functions are part of other packages that must be loaded. If you need to write your own function, this is also possible.
> help(function.name)If help is not given, check the spelling OR you have found a function that is not a part of the BASE package. Refer to the listing of packages given on the R homepage to see a listing of the different add-on packages that are available. Click on any package to see a listing of the functions that are a part of that package. For more detailed information, refer to the Section 5 of the FAQ under the R Homepage Documentation.
Suppose you want to use the function, survreg. This function is available in the package, survival. To load the package, type
> library(survival)You will now be able to use the function, survreg.
Another way to load the package, survival, in Windows, is by left clicking on the Packages menu. The image shown below will appear. Left click on Load package... . This will give you a list of the different packages that you can load. Select the package, survival, and then left click on the OK button. You will now be able to use the function, survreg.

You can find out which functions a package provides by typing
> libarary(help=survival)or
> help(package=survival)
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)
}
The function takes one argument, a vector x,
and returns a vector. The lines beginning with #
are comments. The last line tells the function
to return the value result, i.e. the original
vector x, transformed by subtracting its mean,
and then dividing by its standard deviation.
To invoke the function on a vector x, type
> xstand <- standardize(x)To see the commands which make up the function, just type
> standardizeNote that there are no brackets used. If you want to create a function which returns several outputs, here is a simple example.
sqacu<-function(x)
{
# Inputs: a vector x
# Outputs: the square of x and the cube of x
#
res1<-x^2
res2<-x^3
return(list("square"=res1,"cube"=res2))
}
Now, if you type
>sqacu(2) $square: [1] 4 $cube: [1] 8 >sqacu(2)$square [1] 4
> x <- matrix(1:12,3,4) > apply(x,2,mean) #returns the mean of each column. > apply(x,1,mean) #returns the mean of each row
jsum<-function(x)
{
jsum <- 0
for(i in 1:length(x))
{
jsum <- jsum + x[i]
}
return(jsum)
}
Note that R has its own function that performs this task,
called sum.
It will work MUCH faster than this one, especially on large vectors.