Dr. Mark Gardener


Community Ecology Cover

Community Ecology: Analytical Methods Using R and Excel

Writer's Bloc

On this page you can find out about my latest writing project. I'll post updates on progress, tables of contents and also some of the R scripts (and possibly Excel spreadsheets) I am developing in support of the new book. I'll try to keep the material reasonably up to date.

The Writer's Bloc homepage contains a table of contents and an index of the pages that contain custom R commands and R scripts.

Community Ecology: analytical Methods Using R and Excel

Available now from
Pelagic Publishing

Pelagic Publishing


Chapter 7. Diversity – Species Richness

Calculating species richness from a community data set

function: spec.rich()


Chapter 7. Diversity – Species Richness

Species richness – calculating it

Species richness is simply the number of different species in a sample. Of course it is not quite that simple. One thing I wanted to do early on is to make a simple R script that would show you the number of species for each sample in a community dataset. You can do this using the vegan package in R but it is always nice to do things for yourself because this helps you understand better. The custom function created is called spec.rich().

## Species Richness
## Mark Gardener 2012
## www.dataanalytics.org.uk
spec.rich <- function(data) { # start function
## data = Community data, rows of samples
sr <- vector("numeric") # a blank vector
## Start a loop
for (i in 1:length(rownames(data))) {  # run for as many rows as in data
     x <- length(which(data[i,]>0))    # how many items are > 0, this is richness
     sr[i] <- x                        # set value in a vector
    }                                  # end the loop 
       names(sr) <- rownames(data)    # make names of vector same as original data
   invisible(sr)                      # save result invisibly
      return(sr)                      # show result now
       } # end function
## END    

The script uses a simple loop, which determines how many items are > 0, in other words, the number of species. The result is a simple vector, if the community data contained row names then these are shown in the result:

> spec.rich(gb.biol)
E1 E2 E3 E4 E5 E6 G1 G2 G3 G4 G5 G6 W1 W2 W3 W4 W5 W6
17 14 15 25 21 17 28 22 18 28 26 24 12 11 11 12 12 10

The community data can be a matrix or a data.frame object.


Providing training for:

  • Ecology
  • Data analysis
  • Statistics
  • R The statistical programming language
  • Data management
  • Data mining

Follow me...
Facebook Twitter Google+ Linkedin Amazon
Contact DataAnalytics Homepage