An official website of the United States government.

This is not the current EPA website. To navigate to the current EPA website, please go to www.epa.gov. This website is historical material reflecting the EPA website as it existed on January 19, 2021. This website is no longer updated and links to external websites and some internal pages may not work. More information »

CADDIS Volume 4

R Script: Compute Cumulative Percentiles

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

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

cutoff <- 0.75
                                                           # Select a cutoff percentile
par(mfrow = c(1,3), pty = "s")
                                                           # Specify three plots per page
for (i in 1:length(taxa.names)) {
     csum <- cumsum(dftemp[, taxa.names[i]])/
     sum(dftemp[,taxa.names[i]])
                                                           # Compute cumulative sum
                                                           # of abundances
     plot(dftemp$temp, csum, type = "l", xlab = "Temperature",
          ylab = "Proportion of total", main = taxa.names[i])
                                                           # Make plots like Figure 5
     ic <- 1
     while (csum[ic] < 0.75) ic <- ic + 1
                                                           # Search for point at which
                                                           # cumulative sum is 0.75
     CP[i] <- dftemp$temp[ic]
                                                           # Save the temperature that
                                                           # corresponds to this
                                                           # percentile.
     }
names(CP) <- taxa.names
print(CP)