Search code examples
rvegan

Calculate Gamma diversity over complete dataset using Vegan package in R


I have some datasets for which i want to calculate gamma diversity as the Shannon H index.

Example dataset:

Site    SpecA   SpecB  SpecC
1       4        0     0
2       3        2     4
3       1        1     1

Calculating the alpha diversity is as follows:

vegan::diversity(df, index = "shannon")

However, i want this diversity function to calculate one number for the complete dataset instead of for each row. I can't wrap my head around this. My thought is that i need to write a function to merge all the columns into one and taking the average abundance of each species, thus creating a dataframe with one site contaning all the species information:

site  SpecA   SpecB  SpecC
1     2.6     1      1.6 

This seems like a giant workaround for something that could be done with some existing functions, but i don't know how. I hope someone can help in creating this dataframe or using some other method to use the diversity() function over the complete dataframe.

Regards


Solution

  • library(vegan)
    data(BCI)
    diversity(colSums(BCI)) # vector of sums is all you need
    ## vegan 2.6-0 in github has 'groups' argument for sampling units
    diversity(BCI, groups = rep(1, nrow(BCI))) # one group, same result as above
    diversity(BCI, groups = "a") # arg 'groups' recycled: same result as above