#' Mangrove Indices of Diversity
#'
#' Computes for Shannon species diversity index, Simpsons species diversity index,
#' Pielou species evenness index, and Margalef species richness index.
#'
#'
#'
#' @param data Processed data frame obtained from \code{\link{data_prep}}.
#' @param group.by String or list to categorize at which spatial level of grouping the data analysis should be.
#'
#'
#' @return OUTPUTS should include:
#' 1.[name of cluster].div, a data frame containing the values for diversity indices PER site
#' 2.[name of cluster].div.sum = a data frame containing the values for diversity indices
#' 3. printed output of the summary that you can view in the console
#'
#' @keywords mangrove index of diversity, Shannon diversity, Simpson diversity, Margalef richness, Pielou evenness
#'
#'
#'
#'
#' @export
compute_DIV<-function(data = data,
group.by=group.by){
# Declare variables
x = as.data.frame(data)
clustlvls = group.by
# Defines the `%>%` operator to the current environment
`%>%` <- dplyr::`%>%`
# Make a for loop for computing diversity indices for all given levels of cluster
for (i in 1:length(clustlvls)) {
## Counts the number of recorded individuals per species per subgroup
d.man <- as.data.frame(x %>% dplyr::count(x[[clustlvls[i]]], SITE, Species))
d.man.sum<- as.data.frame(x %>% dplyr::count(x[[clustlvls[i]]], Species))
## This function will transform the data frame into a format readable by the
## diversity function of vegan package and will count number of individuals per species
div.man <- d.man %>% tidyr::spread(Species,n, fill=0)
div.man.sum<- d.man.sum %>% tidyr::spread(Species,n, fill=0)
## Compute for number of species observed
sp<- vegan::specnumber(div.man[,-1])
sp.sum<- vegan::specnumber(div.man.sum[,-1])
## Compute for total number of individuals
N<- apply(div.man[,-c(1,2)],1,sum)
N.sum<- apply(div.man.sum[,-1],1,sum)
## Compute for Shannon index of diversity
div.man$Shannon <- round(vegan::diversity(div.man[-c(1,2)], index="shannon"), digits = 3)
div.man.sum$Shannon <- round(vegan::diversity(div.man.sum[-1], index="shannon"), digits = 3)
## Compute for Simpson index of diversity
div.man$Simpson<- round(vegan::diversity(div.man[-c(1,2, ncol(div.man))], index="simpson"), digits = 3)
div.man.sum$Simpson<- round(vegan::diversity(div.man.sum[-c(1, ncol(div.man.sum))], index="simpson"), digits = 3)
## Compute for Pielou index of evenness
div.man$Pielou<- round(div.man$Shannon/log(sp), digits = 3)
div.man.sum$Pielou<- round(div.man.sum$Shannon/log(sp.sum), digits = 3)
## Compute for Margalef index of richness
div.man$Margalef <-round((sp-1)/log(N), digits = 3)
div.man.sum$Margalef <-round((sp.sum-1)/log(N.sum), digits = 3)
## Convert non numerics to zeroes again
div.man[is.na(div.man)] = 0
div.man.sum[is.na(div.man.sum)] = 0
## Renames first column
colnames(div.man)[1]<- clustlvls[i]
colnames(div.man.sum)[1]<- clustlvls[i]
## Selects the first column (cluster) and the last four columns (indices)
### and outputs them in a data frame
assign(paste(tolower(clustlvls[i]), "div", sep="."),
div.man[c(1,2,(ncol(div.man)-3):(ncol(div.man)))],
pos = .GlobalEnv)
assign(paste(tolower(clustlvls[i]), "div.sum", sep="."),
div.man.sum[c(1,(ncol(div.man.sum)-3):(ncol(div.man.sum)))],
pos = .GlobalEnv)
# This will print the result to the console
cat("\n")
cat("\n Per",tolower(clustlvls[i]),"summary of indices: \n")
print.noquote(div.man.sum[c(1,(ncol(div.man.sum)-3):(ncol(div.man.sum)))],
row.names=FALSE)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.