Individual Based Rarefaction using R-package

Jenna Jacobs -

Updated May 2011

Rarefaction is a method for comparing species richness between treatments after standardizing to take in account sampling effort.  More information is available in Gotelli & Colwell (2001) or Buddle et al (2005).  Here I have created a simple function in R that will allow you to quickly and easily create individual based rarefaction curves and a simple plot to explore the data.


I) Installing R-package and vegan package from CRAN
II) Preparing Data and importing into R
III) Rarefaction in R

IV) Hints and tricks
V) Notes and Problems
VI) References

I) Installing R-package and vegan library from CRAN

1) Download and install the setup file from the r-project website

2) Open R and install the mvpart package by selecting Install package(s) from CRAN...

3) Select vegan from the list and click OK

Although there are many wonderful resources for working in R package on the web (Statistics with R is one of my favorites)  The best trick I have learned is using a script to organize you commands.  This allows you to import your data once, and keep a record of the entire analysis if you ever need to replicate the analysis or use a similar analysis for another data set.  This done simply by opening a "new scipt" under the file menu.  The commands are run simply by highlighting them a using control-r.  I often use the # character to denote lines I do not want to run.

I) Preparing Data and importing into R

1) In Excel create a table with treatments (each line on the rarefaction plot) as row headings and species as column headings.  (If your data is opposite (species in rows, treatments in columns) see number 2 in the hints and tricks section).
2)Save the file as a .csv (comma separated values), by choosing "Save as" from the File menu, and change Save as type to "CSV(Comma delimited)"

You are now ready to import your data into R
3) Open the R program
4) Change the working directory to where you saved you .csv file from step 2 by selecting "Change dir" from the file menu and browsing to the proper directory or use the command setwd

#here I set my working directory to my desktop, note the double slashes
setwd("C:\\Documents and Settings\\yourlogin\\Desktop")

5) In order to import your file into R you need to use the read.csv command.  To find out more about this command type "?read.csv" without the quotation marks at the pompt.

name<- read.csv("filename.csv", row.names=1)
# name: the name of the file in R
# filename: the name you called your file in excel
# row.names=1, use this if the first column of your data are the names of your rows (i.e."Sample 1")


III) Rarefaction in R
           1) The rarefaction function is basically a loop script of the rarefy function in the vegan package. 
                You can either download the rarefaction function by RIGHT clicking here, and save the target to a folder. 
                I put mine in a folder called "functions" in my R folder eg. C:\Program Files\R\functions, or source the package directly from my website.

2) Load the vegan package by selecting "Load Package..." from the package menu. Then select "vegan" and click ok.
    Alternatively you can type >library(vegan)
3) Source the rarefaction function by typing
        source("C:\\Program Files\\R\\functions\\rarefaction.txt")  #or wherever you saved it.
        source("") #source the function from the website.

4)The rarefaction function is rarefaction(x, subsample=5, plot=TRUE, color=TRUE, error=FALSE,  legend=TRUE, symbol)
                where x= the datamatrix
                subsample= the interval betwwen subsamples (default is 5)  
                plot= if you want the function to plot the results
                color= if you want the plot in color (TRUE) or black and white (FALSE)
                error=  if you want the plot to show the iterative error around the mean
                legend= if you want a legend
                symbol= a vector of symbols to correspond with treatments [eg. c(1,2,3)]

#Try this example dataset from Work et al (2010):
# 1) import the emend dataset from my website
emend<-read.csv("", row.names=1)

# 2) load the vegan library

# 3) The the rarefaction function
source("C:\\Program Files\\R\\functions\\rarefaction.txt")


# 4) Rarefy your dataset
emend.rare<-rarefaction(emend, col=F) # I'm a big fan of B&W


5) The rarefaction function creates 3 objects to access them type

emend.rare$richness # a matrix of the mean richness at each subsample
emend.rare$SE  # a matrix of the iterative SE of the mean richness at each subsample
emend.rare$subsample # the subsample sizes used to find the means

You can then use these files to create pretty graphs in R or if you must you can export then with

write.csv(emend.rare$richness, file="EMENDrichness.csv")


IV) Hints and tricks
 1) R is pretty cool at deciphering short hand, you can use rarefaction(x, subsample=5), or rarefactions(x, subs=5) or even rarefactions(x, 5).
            2) If your data has species in rows and treatments in columns use the t(x) command
                    >rarefaction(t(x), c(subsample))

V) Notes and Problems

        If you experience any problems with this code I would really like to hear about them  please send an email to 

VI) References
Buddle, C.M., Beguin, J., Bolduc, E., Mercado, A., Sackett, T.E., Selby, R.D., Varady-Szabo, H., and Zeran, R.M. 2005. The importance and use of taxon sampling curves for comparative biodiversity research with forest arthropod assemblages. Can. Entomol. 137: 120-127.

Gotelli, N. J., and R. K. Colwell. 2001. Quantifying biodiversity: Procedures and pitfalls in the measurement and comparison of species richness. Ecology Letters 4:379-391.

Work TT, Jacobs JM, Spence JR, Volney WJ (2010) Increased green-tree retention required to maintain ground beetle biodiversity in boreal mixedwood forests Ecological Applications 20: 741-751.