# Shannon Diversity Index – The Diversity Function in R

Determining population diversity is an important part of the statistics of an ecosystem. The Shannon diversity index is one of several statistical indices used to measure species richness. There are several other diversity indices such as Simpson’s index and the Shannon Weiner index, however. Shannon’s index will be our main focus.

### What is the Shannon Diversity Index?

The Shannon index is a mathematical tool for calculating the proportional abundance of species in a given location. This type of species abundance provides an indication of the biological diversity in that area. It is related to information entropy in that the formula Of the two concepts is identical. The difference is that in information entropy the value p is the probability of a particular arrangement but with the Shannon index p = n/N where “n” is the number of individuals of a given species and N is the total number of individuals.

### Description of the Function

The diversity() function has the form of diversity(x, index) and it produces the indicated diversity index.<ul>
<li>”x” is the date of being evaluated.</li>
<li>”index” is the diversity index being used.</li>
</ul> The diversity indices available for this function are “shannon”, “Simpson” or “invsimpson”. Each of these indices produces different values, providing different information on the biodiversity of the area under study. While you can make these calculations by simply encoding the formula for the index you are using, this function simplifies the process, by putting it into a single formula.

Key features of the function. This function is able to deal with the relative abundance of different zoological taxa. Its strength is that it can be used with different indexes. It can be used to determine, both animal and plant diversity. It can be used to check species evenness and calculate the maximum diversity possible in the area under study. It can also be used to study the genetic diversity within a single species or other taxa. Being able to use three different indexes produces a degree of flexibility that you do not have with a simple Plug and calculate function.

### Examples of the Diversity Function in Action

The diversity() function is easy to use, but it does have variations. Here are two examples, that use Shannon’s index. However, before you can try these examples you need to run the following code loading the package containing this function.

install.packages(‘vegan’)
library(vegan)

> pop = c(100, 500, 200, 600, 500, 400, 700, 900, 800)
> D = diversity(pop)
> D
 2.066992

In this example, we simply put the vector to be evaluated into the diversity() function with the index Shannon’s by default

> pop = c(100, 500, 200, 600, 500, 400, 700, 900, 800)
> D = diversity(pop,”shannon”)
> D
 2.066992

In this example Shannon’s index a specified, however, the result is the same because they are applying the same index.

### An alternative way to find the Shannon Diversity Index

In this example, we have an alternative set of code that calculates Shannon’s index.

> pop = c(100, 500, 200, 600, 500, 400, 700, 900, 800)
> n = pop
> N = sum(pop)
> p = n/N
> H = -sum(p*log(p))
> H
 2.066992

Here we simply set up the formula for Shannon’s index. The advantage of doing it this way is that it helps you to better understand how it works.

While the diversity() function is a useful function, it does require downloading a new package. The same results can be obtained with a fairly simple amount of code. Both approaches have advantages. The one you use depends on which will fit your needs best.

Scroll to top