Last active
January 7, 2021 08:30
-
-
Save BlasBenito/4c3740b056a0c9bb3602f33dfd35990c to your computer and use it in GitHub Desktop.
Functions to compute Simpson's and Sorensen's betas from taxa lists from different sites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#returns biodiversity components a, b, and c from two vectors with taxa names. | |
#x: character vector, taxa list of one site | |
#y: character vector, taxa list of another site | |
abc <- function(x, y){ | |
#list to store output | |
out <- list() | |
#filling the list | |
out$a <- length(intersect(x, y)) | |
out$b <- length(setdiff(x, y)) | |
out$c <- length(setdiff(y, x)) | |
#returning the output | |
out | |
} | |
#returns the Sorensen's beta from the output of abc() | |
sorensen_beta <- function(x){ | |
x$bsor <- round(2 * x$a / (2 * x$a + x$b + x$c), 3) | |
x | |
} | |
#returns the Simpson's beta from the output of abc() | |
simpson_beta <- function(x){ | |
x$bsim <- round(min(x$b, x$c) / (min(x$b, x$c) + x$a), 3) | |
x | |
} | |
#returns the biodiversity components a, b, and c and the betadiversity indices of Sorensen and Simpson | |
#x: character vector, taxa list of one site | |
#y: character vector, taxa list of another site | |
betadiversity <- function(x, y){ | |
require(magrittr) | |
abc(x, y) %>% | |
sorensen_beta() %>% | |
simpson_beta() | |
} | |
#computes betadiversity indices for a set of sites of an arbitrary size stored in a long-format data frame | |
#x: data frame with at least two columns: site.column, with the site name, and taxa.column, with the taxon name. The data frame ought to have one row per site and taxon. | |
#site.column: character, name of the column in x with the site names. | |
#taxa.column: character, name of the column in x with the taxa names. | |
betadiversity_multisite <- function( | |
x, | |
site.column, #column with site names | |
taxa.column #column with taxa names | |
){ | |
#get site combinations | |
site.combinations <- utils::combn( | |
x = unique(x[, site.column]), | |
m = 2 | |
) | |
#iterating through site pairs | |
betadiversity.df <- foreach::foreach( | |
i = 1:ncol(site.combinations), | |
.combine = 'rbind' | |
) %do% { | |
#site names | |
site.one <- site.combinations[1, i] | |
site.two <- site.combinations[2, i] | |
#getting taxa lists | |
taxa.list.one <- x[x[, site.column] %in% site.one, taxa.column] | |
taxa.list.two <- x[x[, site.column] %in% site.two, taxa.column] | |
#betadiversity | |
beta <- betadiversity( | |
x = taxa.list.one, | |
y = taxa.list.two | |
) | |
#adding site names | |
beta$site.one <- site.one | |
beta$site.two <- site.two | |
#returning output | |
beta | |
} | |
#remove bad rownames | |
rownames(betadiversity.df) <- NULL | |
#reordering columns | |
betadiversity.df <- betadiversity.df[, c( | |
"site.one", | |
"site.two", | |
"a", | |
"b", | |
"c", | |
"bsor", | |
"bsim" | |
)] | |
#returning output | |
betadiversity.df | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment