Environmental Limits: R Script

We can use a `for` loop similar to that used for weighted averages to compute environmental limits (Equation 2) for different taxa. In this method, you must select a value of the cumulative percentile that you wish to use for computing the environmental limit. In the script below, the `cutoff` value is selected to be 0.75.

First make sure that you have loaded the sample biological and environmental data and merged them into a single data frame called `dfmerge`.

Also make sure that you have selected the taxa for which you wish to calculate environmental limits and saved them in the vector `taxa.names` (information on defining `taxa.names`).

```# Define a storage vector for the cumulative percentile
CP <- rep(NA, times = length(taxa.names))

# Sort sites by the value of the environmental variable
dftemp <- dfmerge[order(dfmerge\$temp), ]

# Select a cutoff percentile
cutoff <- 0.75

# Specify three plots per page
par(mfrow = c(1,3), pty = "s")
for (i in 1:length(taxa.names)) {
# Compute cumulative sum of abundances
csum <- cumsum(dftemp[, taxa.names[i]])/
sum(dftemp[,taxa.names[i]])

# Make plots like Figure 4
plot(dftemp\$temp, csum, type = "l", xlab = "Temperature",
ylab = "Proportion of total", main = taxa.names[i])

# Search for point at which cumulative sum is 0.75
ic <- 1
while (csum[ic] < 0.75) ic <- ic + 1

# Save the temperature that corresponds to this percentile.
CP[i] <- dftemp\$temp[ic]

}
names(CP) <- taxa.names
print(CP)```

Top of page

R Scripts:   Overview    Previous    Next