This function calculates the Shannon-Weiner diversity index, a measure of species diversity based on species relative abundance.

shannon_weiner(x)

Arguments

x

A character vector of species names. Each element represents an individual of a species.

Value

A numeric value representing the Shannon-Weiner diversity index. Returns NA if the input vector is empty or contains fewer than two unique species.

Note

The Shannon-Weiner index is undefined for an empty vector or vectors with only one unique species. For such cases, the function will return NA or 0 as appropriate.

Examples

# Example 1: Calculate Shannon-Weiner index for a dataset
x <- c("jonesi","gambiensis","hispida","hispida","thebaica","helvum","helvum","gambianus","gambianus","pusillus","monstrosus","aegyptiacus")
shannon_weiner(x)
#> [1] 2.138333

# Example 2: Handle edge cases
shannon_weiner(c("jonesi")) # Returns 0
#> [1] 0
shannon_weiner(character(0)) # Returns NA with a warning
#> Warning: Input vector is empty. Returning NA.
#> [1] NA