Learn R Programming

sprex (version 1.4.2)

diversity: Diversity Indices

Description

Calculate common diversity and entropy indices.

Usage

diversity(
  x,
  type = c("effective.number", "richness", "shannon", "simpson", "gini.simpson",
    "unb.gini", "eveness.simpson", "eveness.pielou", "inv.simpson", "renyi", "hill"),
  q = NULL
)

Value

if a vector is supplied for x, a single value for the chosen type of index. If a matrix, a vector values for each column.

Arguments

x

vector or matrix of values (character, factor) representing a class, from which proportions will be computed. If numeric, values will be converted to proportions. If a matrix, indices will be computed for all columns.

type

type of index to compute. See Details for descriptions. If "renyi" or "hill", then "q" must be specified.

q

order of Hill number (must be >= 0).

Author

Eric Archer eric.archer@noaa.gov

Examples

Run this code
x <- sample(letters[1:4], 100, replace = TRUE, p = c(1, 2, 3, 4))

types <- c("richness", "effective.number", "shannon",
  "simpson", "inv.simpson", "gini.simpson", "unb.gini",
  "eveness.simpson", "eveness.pielou"
)

sapply(types, function(tp) diversity(x, type = tp))

# hill numbers with increasing order
order <- 0:5
hill.num <- sapply(order, function(q) diversity(x, type = "hill", q = q))
hill.num
plot(order, hill.num, type = "b")

# a matrix of frequencies
spp.freq <- cbind(
  sample(letters[1:4], 100, replace = TRUE, p = c(1, 1, 1, 4)),
  sample(letters[1:4], 100, replace = TRUE, p = c(4, 1, 1, 1)),
  sample(letters[1:4], 100, replace = TRUE, p = c(1, 1, 1, 1))
)

diversity(spp.freq, type = "eff")

Run the code above in your browser using DataLab