Learning R

Best Practices to Keep in Mind

# 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

# Install 'swirl' package from your chosen CRAN server
> install.packages("swirl")

# Load and execute 'swirl' training
> library(swirl)
> swirl()

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
> rm(variable)

Graphics

Statistical Testing and Modeling

Ecological Models

Spatial Statistics, GIS, and Remote Sensing