R/shannon.R

Defines functions shannon

Documented in shannon

#' @describeIn allindices Shannon index

shannon <- function(x, base = exp(1), tax_lev = "Taxa") {

  # check if the object x is of class "biomonitoR"
  classCheck(x)

  # get the data.frame at the desired taxonomic level
  DF <- x[[tax_lev]]

  if (inherits(x, "bin")) {
    DF <- to_bin(DF)
  }

  # remove unassigned row from the species count if present
  if ("unassigned" %in% DF[, 1]) {
    z <- which(DF[, 1] == "unassigned")
    DF <- DF[-z, ]
  }

  # apply the function for calculating the Shannon index that is stored in the Pi.R file
  sha <- apply(DF[, -1, drop = FALSE], 2, FUN = function(x) {
    Pi(x, index = "Shannon", base = base)
  })
  sha
}
alexology/biomonitoR documentation built on April 7, 2024, 10:15 a.m.