EE BIOL C177/C234
Follow this installation guide to get started.
You need two things:
R alone = powerful but clunky
RStudio = a sleek interface that makes everything easier
For this course, RStudio is our primary IDE.
Two quick wins:
<-, |>, != beautiful
styler package β auto-format to tidyverse style (link)The classic first program:
R as a calculator:
Why <- instead of =?
<- is for assignment (βput this value in that boxβ)= is for function arguments (mean(x = c(1,2,3)))β key!
Create y = 5 Γ 3, then z = y / 2:
Donβt memorize every function β let AI help!
But always verify AI suggestions:
Install once, load every session:
Quirk Alert π€
install.packages("ggplot2") β quotes
library(ggplot2) β no quotes
Yes, itβs inconsistent. Welcome to R.
pak Method (Recommended) β# install.packages("pak") # one-time setup
pak::pak("ggplot2") # from CRAN
pak::pak("cttobin/ggthemr") # from GitHub
pak::pak(c("dplyr", "tidyr")) # multiple at onceWhy pak?
Weβll use pak throughout this course.
:: Operatorpak::pak() means: βuse the pak function from the pak packageβ
This lets you call a function without loading the whole package β avoiding name conflicts (a huge source of hidden bugs!).
The tidyverse is an opinionated collection of R packages designed for data science.
Nine packages loaded at once:
| Package | Purpose |
|---|---|
ggplot2 |
Data visualization |
dplyr |
Data manipulation |
tidyr |
Data tidying |
readr |
Reading data |
purrr |
Functional programming |
tibble |
Modern data frames |
stringr |
String manipulation |
forcats |
Factor handling |
lubridate |
Date/time handling |
The filter and lag conflicts can cause silent bugs:
library(conflicted)
library(tidyverse)
conflict_prefer("filter", "dplyr")
conflict_prefer("lag", "dplyr")Or use the explicit package::function() syntax:
Want to avoid the annoying messages?
You can use the suppressPackageStartupMessages() function to hide the start-up output when loading packages:
print(), arithmetic, <-? for helppakNext up: Data structures β vectors and tibbles! π§
Basics of R and RStudio