R tips
These pages provide an introduction to R, emphasizing topics in data analysis that are covered in the graduate course, Biol 501: Quantitative methods in ecology and evolution.
Get R
Download R from the CRAN website.
Mac OS X users: If you are using R version 2, it is recommended that you also install the tcltk tools package (http://cran.r-project.org/bin/macosx/tools/). This package is already included with R version 3.
Get add-on packages
R has a core set of command libraries (base, graphics, stats, etc), but there is a wealth of add-on packages available (the full list is available at the CRAN web site).
Packages already included
The following are a few of the add-on packages already included with your standard R installation.
boot – bootstrap resampling
foreign – read data from files in the format of other stats programs
lattice – multi-panel graphics
MASS – software and data associated with the book by Venables and Ripley:
Modern Applied Statistics with S-PLUS
mgcv – generalized additive models
nlme – linear mixed-effects models, generalized least squares
To use one of them you need to load it,
library(packagename)
You’ll have to do this again every time you run R.
To see all the libraries available on your computer enter
library()
Example packages available for download
Most R packages are not included with the standard installation, and you need to download and install it before you can use it. Here are a few add-on packages that might be useful in ecology and evolution. The full list of available packages is here.
ape – phylogenetic comparative methods
biodiversityR – statistical analysis of biodiversity patterns
leaps – all subsets regression
meta – meta-analysis
mra – analysis of mark-recapture data
multcomp – multiple comparisons for linear models
popbio – analyzing matrix population models
pwr – power analysis
Rcmdr – graphical user interface (menus, buttons) for basic stats in R
qtl – QTL analysis
shapes – geometric morphometrics
vegan – ordination methods for community ecology
To install one of these packages use the menu bar in R. Select “Install packages” under the “Packages” menu item. You’ll have to select a download site (Canada BC). Then select your package from the list provided.
Or, execute the following command instead of using the menu,
install.packages("packagename",dependencies=TRUE)
To use a package once it is installed, load it by entering
library(packagename)
R is under constant revision, and periodically it is a good idea to install the latest version. Once you have accomplished this, you should also download and install the latest version of all the add-on packages too.
Get help
Built-in help
Use “?” in the R command window to get documentation of specific command. For example, to get help on the “mean” function to calculate a sample mean, enter
?mean
You can also search the help documentation on a more general topic using “??” or “help.search”. For example, use the following commands to find out what’s available on anova and linear models.
??anova
??"linear models" # same as help.search("linear models")
A window will pop up that lists commands available and the packages that include them. To use a command indicated you might have to load the corresponding library. (See “Add-on packages” for help on how to load libraries.) Note the “??” command will only search documentation in the R packages installed on your computer.
Interpreting a help page
As an example, here’s how to interpret the help page for the sample mean, obtained by
?mean
In the pop-up help window, look under the title “Usage” and you will see something like this:
mean(x, trim = 0, na.rm = FALSE, ...)
The items between the brackets “()” are called arguments.
Any argument without an “=” sign is required — you must provide it for the command to work. Any argument with an “=” sign represents an option, with the default value indicated. (Ignore the “…” for now.)
In this example, the argument “x” represents the data object you supply to the function. Look under “Arguments” on the help page to see what kind of object R needs. In the case of the mean almost any data object will do, but you will usually apply the function to a vector (representing a single variable).
If you are happy with the default settings, then you can use the command in its simplest form. If you want the mean of the elements in the variable “myvariable”, enter
mean(myvariable)
If the default values for the options don’t meet your needs you can alter the values. The following example changes the “na.rm” option to TRUE. This instruct R to remove missing values from the data object before calculating the mean. (If you fail to do this and have missing values, R will return “NA”.)
mean(myvariable, na.rm=TRUE)
The following example changes the “trim” option to calculate a trimmed mean,
mean(myvariable, trim=0.1)
Online help
Several excellent R books are available free to UBC students online through the UBC library. Check the “books” tab on the main course page.
Tom Short’s R reference card
Venables and Smith’s Introduction to R
(pdf file — right-click and save to disk)
Kuhnert and Venables’ An Introduction to R: Software for Statistical Modelling & Computing
(large pdf file: right-click and save to disk)
Someone has solved your problem already
If you want to accomplish something in R and can’t quite figure out how, and your books aren’t helping, chances are that someone has already solved the problem and the answer is sitting on a web page somewhere on the internet. Google or the R project Search Engine might find it for you.