This function calculates Margalef's diversity index, a measure of species richness relative to the total number of individuals, using species names.
margalef(x, details = FALSE)
Margalef, R. (1958) Information Theory in Ecology. General Systems, 3, 36-71.
If return_details = FALSE
(default), a numeric value representing Margalef's diversity index.
If return_details = TRUE
, a list with the following components:
Margalef_Index
: The calculated Margalef index.
S
: The number of unique species.
N
: The total number of individuals.
Returns NA
if the index is undefined (e.g., fewer than two species or individuals).
Margalef's index is undefined if there is only one species or individual.
The function will return NA
in such cases with a warning.
# Example 1: Calculate Margalef's index for a simple dataset
x <- c("jonesi","gambiensis","hispida","hispida","thebaica","helvum","helvum","gambianus","gambianus","pusillus","monstrosus","aegyptiacus")
margalef(x) # Simple output
#> [1] 3.219437
# Example 2: Return detailed results
margalef(x, details = TRUE)
#> $margalef_index
#> [1] 3.219437
#>
#> $S
#> [1] 9
#>
#> $N
#> [1] 12
#>
# Example 3: Handle edge cases
margalef(c("gambiensis")) # Returns NA with a warning
#> Warning: Margalef index is undefined for fewer than 2 species or individuals
#> [1] NA