R/shannon.R

Defines functions shannon

Documented in shannon

#' Get alpha diversity using shannon
#'
#' @param x A list of counts
#' @return A single value
#'
#' @examples
#' shannon(seq_len(10))
#'
#' @export

# x: Species count vector
shannon <- function(x) {

    # Ignore zeroes
    x <- x[x > 0]

    # Species richness (number of species)
    S <- length(x)

    # Relative abundances
    p <- x/sum(x)

    # Shannon index
    (-sum(p * log(p)))
}

Try the animalcules package in your browser

Any scripts or data that you put into this service are public.

animalcules documentation built on Nov. 8, 2020, 6:47 p.m.