Jump to main content or area navigation.

Contact Us

CADDIS Volume 4: Data Analysis

Predicting Environmental Conditions from Biological Observations (PECBO) Appendix

Central Tendencies: R Script

In R, the basic formula for computing a central tendency for a taxon (Equation 1) can be represented in general as follows:

WA <- sum(Y*x)/sum(Y)

where Y is a vector containing the abundance of the taxon of interest for each sample. Y can also contain presence/absence data coded as 1 for present and 0 for absent. x is vector containing the value of the environmental variable of interest for each sample, and sum computes the sum of the values of a numerical vector.

A for loop can be used to create weighted averages for many different taxa from previously loaded data. To run this script, first make sure that you have loaded the sample biological and environmental data and merged them into a single data frame called dfmerge.

Designate the taxa for which you want to compute tolerance values.

taxa.names <- c("ACENTRELLA", "DIPHETOR", "AMELETUS")

Then, run a for loop that repeats the weighted average computation for each of the selected taxa.

# Define WA to be vector of length the same as the 
# number of taxa of interest
WA <- rep(NA, times = length(taxa.names))

for (i in 1:length(taxa.names)) {
	WA[i] <- sum(dfmerge[,taxa.names[i]]*dfmerge$temp)/
                 sum(dfmerge[,taxa.names[i]])
}
# Name each of the elements of the vector by their taxa name
names(WA) <- taxa.names
print(WA)

Top of page


R Scripts:   Overview    Previous    Next

Jump to main content.