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)

Source

Margalef, R. (1958) Information Theory in Ecology. General Systems, 3, 36-71.

Arguments

x

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

details

Logical. If `TRUE`, the function returns a list containing the Margalef index, the number of unique species (S), and the total number of individuals (N). Defaults to FALSE.

Value

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).

Note

Margalef's index is undefined if there is only one species or individual. The function will return NA in such cases with a warning.

Examples

# 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