Many of the analyses underlying the projects found throughout this site are conducted using the R statistical programming language. A coding approach to statistics allows automation for large or repetitive datasets, improves reproducibility, and provides documentation for scientific peer review. R is great for this because it is:
- Open source (AKA free!) and operating system-agnostic (no Windows vs Mac wars here)
- Flexible with a large user base constantly developing new tools
- Very common in biology, marine science, environmental science, and other disciplines
However, R does have a steep learning curve. We are used to operating graphical interfaces with dropdown menus, fillable fields, and buttons, so a programming approach (and all of the quirks that come along with that) takes some getting used to. It doesn’t help that many of the errors R generates work your code breaks can be unintelligible. When in doubt, your search engine of choice can be an excellent error message translator. There are also numerous websites available for learning R from beginner to expert and tailored to specific disciplines or types of analyses.
A running list of some of these resources and best practices tips are included below. These are resources I have used in the past, but if there are big ones I’ve missed that you think would be helpful here for others, reach out! Code demos and tutorials for common analyses will be added in the future.
For any code chunks below, italicized text requires you to replace the item with your variable or path of interest and “quoted text” requires the bounding quotations (” or ‘ work, but they must be paired [“…” or ‘…’]).
Best Practices to Keep in Mind
- Keep related R files and data together in dedicated folders and R Projects. This simplifies sharing and review of your work since you, your collaborators, nor R have to look all over your system for specific elements
- Create a folder on your computer (using whatever organization system you use) and store relevant data in this one location
- In the RStudio IDE, select File > New Project or the “New Project” icon below the Edit dropdown menu. Save the project to the dedicated folder.
- You can save multiple R scripts, Markdowns, etc. to a single project. When you open a project, it will load all of its contents into RStudio simultaneously.
- Use <- instead of = when assigning objects. Both will work in R but = can get confused with testing for equivalence (==).
- Give your variables meaningful names. It will help you, your collaborators, or any reviewers follow along with what you are trying to do.
- Use camelCase, under_scores, or per.iods for long variable names to improve legibility. somelongvariablenamehere can be difficult for you or a collaborator to read.
- R ignores white space, so use it to your advantage and improve your code’s legibility.
- R does not ignore capitalization, however. “Art” is not the same as “art” to R.
- Comment (#) throughout your code often! It may be months or years before you return to a given project. Comments will help you get back up to speed quickly.
- It can be useful to attach datasets, as this allows you to reference columns without having to specify the dataset directly. However, you must remember to detach the dataset before moving on to another analysis. If the two datasets have common column or row names, R will default to the attached dataset, which may not be what you intended.
# Dataset shipped with R containing the speed (mph) and stopping distance (ft) of cars from the 1920s
> attach(cars)
# Create a histogram of car speeds from the 'cars' dataset
> hist(speed, xlim = c(0, 25), xlab = "Speed (mph)", ylim = c(0, 20), main = "")
# Remove 'cars' dataset
> detach(cars)
# Create a histogram of car speed without attaching the dataset
> hist(cars$speed, xlim = c(0, 25), xlab = "Speed (mph)", ylim = c(0, 20), main = "")
Getting started with R
- Download R and the IDE RStudio from Posit: https://posit.co/download/rstudio-desktop/
- Cheatsheets for common R Packages: https://posit.co/resources/cheatsheets/
- Online courses (some are free, some are paid)
- Webinars, Videos, and Tutorials
- Posit: https://posit.co/resources/videos/
- Plotly: https://plotly.com/r/
- swirl package: 3-4 hours worth of basic R tutorials conducted within the console
# Install 'swirl' package from your chosen CRAN server
> install.packages("swirl")
# Load and execute 'swirl' training
> library(swirl)
> swirl()
- Books (be sure to look for latest versions)
- The R Book: https://www.wiley.com/en-es/The+R+Book,+3rd+Edition-p-9781119634324
- R for Data Science: https://r4ds.hadley.nz/
- R in Action: https://www.manning.com/books/r-in-action
- Art of R Programming: https://nostarch.com/artofr.htm
- Learning R: https://www.oreilly.com/library/view/learning-r/9781449357160/
Basic Housekeeping
- Working Directory: See where R is currently looking for files and processing your code, or specify where R should look:
> getwd() # Print current directory
> setwd("enter file path") # Tell R to work in a specific location
- Remove unwanted items: This may be necessary to reduce clutter, free up memory, etc.
> rm(variable)
Graphics
- Webinars, Videos, Tutorials, and Workshops
- Plotly: https://plotly.com/r/
- ggplot2: https://ggplot2.tidyverse.org/
- A Step-by-Step Guide to Making 3D Maps with Satellite Imagery in R: https://www.tylermw.com/a-step-by-step-guide-to-making-3d-maps-with-satellite-imagery-in-r/
- Shaded Relief Maps in R: https://jonathanvsv.github.io/Ppage2/blog/shaded-relief-maps-in-r/
- Books (be sure to look for latest versions)
- R Graphics Cookbook: https://r-graphics.org/
- Modern Data Visualization with R: https://rkabacoff.github.io/datavis/
Statistical Testing and Modeling
- Webinars, Videos, Tutorials, and Workshops
- Generalized Additive Models in R, Centre de la Science de la Biodiversite du Quebec: https://r.qcbs.ca/workshop08/book-en/index.html
- Books (be sure to look for latest versions)
- Practical Statistics for Data Scientists: 50+ Essential Concepts Using R and Python: https://www.oreilly.com/library/view/practical-statistics-for/9781492072935/
- Mixed Effects and Models and Extensions in Ecology with R: https://www.highstat.com/index.php/books2?view=article&id=17&catid=18
- Introduction to Bayesian Methods in Ecology and Natural Resources: https://link.springer.com/book/10.1007/978-3-030-60750-0
Ecological Models
- Ecological Forecasting Initiative by the Ecological Society of America (ESA): https://ecoforecast.org/
- National Ecological Observatory Network (NEON) by the National Science Foundation (NSF): https://www.neonscience.org/resources
- Webinars, Videos, Tutorials, and Workshops
- R for Ecology: https://www.rforecology.com/
- Books (be sure to look for latest versions)
- A Primer of Ecology with R: https://link.springer.com/book/10.1007/978-0-387-89882-7
- Ecological Models and Data in R: https://press.princeton.edu/books/hardcover/9780691125220/ecological-models-and-data-in-r
- Occupancy Estimation and Modeling: https://www.sciencedirect.com/book/9780124071971/occupancy-estimation-and-modeling
- Habitat Suitability and Distribution Models with Applications in R: https://www.cambridge.org/core/books/habitat-suitability-and-distribution-models/A17F74A3418DBF9ADA191A04C35187F9
- Spatial Ecology and Conservation Modeling: Applications in R: https://link.springer.com/book/10.1007/978-3-030-01989-1
Spatial Statistics, GIS, and Remote Sensing
Note that the ‘rgeos’ and ‘rgdal’ packages were retired in 2023. Any packages depending on these will not work going forward, and older websites, books, etc. may contain code which no longer functions. Newer packages (e.g., ‘sf’, ‘terra’) should be used in place of the older packages (e.g., ‘sp’, ‘raster’) if or until these packages can be updated.
- Books (be sure to look for latest versions)
- Spatial Statistics for Data Science: Theory and Practice with R: https://www.paulamoraga.com/book-spatial/
- Spatial Point Patterns: Methodology and Applications with R: https://www.routledge.com/Spatial-Point-Patterns-Methodology-and-Applications-with-R/Baddeley-Rubak-Turner/p/book/9781482210200
- Spatial Data Science with Applications in R: https://r-spatial.org/book/
- Geocomputation with R: https://r.geocompx.org/
- Geographic Data Science with R: Visualizing and Analyzing Environmental Change: https://bookdown.org/mcwimberly/gdswr-book/
- Spatial Modeling in GIS and R for Earth and Environmental Sciences: https://www.sciencedirect.com/book/9780128152263/spatial-modeling-in-gis-and-r-for-earth-and-environmental-sciences
- Geographical Data Science and Spatial Data Analysis: An Introduction to R: https://uk.sagepub.com/en-gb/eur/geographical-data-science-and-spatial-data-analysis/book260671