This function calculates Simpson's diversity index, which measures the probability that two individuals randomly selected from a sample will belong to different species.
simpson(x)
A numeric value representing Simpson's diversity index. Returns NA
if the input vector
is empty or contains fewer than two unique species.
Simpson's index is undefined for an empty vector or vectors with only one unique species.
The function will return NA
in such cases with a warning.
# Example 1: Calculate Simpson's diversity index
x <- c("jonesi","gambiensis","hispida","hispida","thebaica","helvum","helvum","gambianus","gambianus","pusillus","monstrosus","aegyptiacus")
simpson(x)
#> [1] 8
# Example 2: Handle edge cases
simpson(c("pusillus")) # Returns NA with a warning
#> Warning: Simpson's index is undefined for fewer than 2 species. Returning NA.
#> [1] NA
simpson(character(0)) # Returns NA with a warning
#> Warning: Input vector is empty. Returning NA.
#> [1] NA