Phase 1: R Fundamentals for Bioinformatics
A 20-Page Beginner's Guide (Designed for Clarity)
Page 1: Welcome to R!
Hello, Scientist!
Imagine R is a magical toolbox. Just like Harry Potter has a wand, you have R to analyze biological data (like DNA sequences or gene patterns). We’ll start with baby steps!
What You’ll Learn:
- How to talk to R (typing commands).
- How to store data (like putting toys in labeled boxes).
- How to make graphs (like drawing cool pictures).
Page 2: Installing Your Tools
Step 1: Download R
- Go to cran.r-project.org.
- Click "Download R for Windows/Mac/Linux" (like picking your game console).
Step 2: Download RStudio
🔍 Think of it like:
- R = Car engine.
- RStudio = Steering wheel + dashboard.
Page 3: Your First R Conversation
Opening RStudio:
- Open RStudio. You’ll see 4 panels:
- Script Window (top-left): Your notepad for commands.
- Console (bottom-left): Where R talks back.
- Environment (top-right): Shows saved data.
- Files/Plots (bottom-right): Where graphs appear.
Type Your First Spell:
In the Console, type:
2 + 2
Press Enter. R replies [1] 4!
🎉 You just did math with R!
Page 4: Storing Data in "Boxes" (Variables)
Variables are like labeled toy boxes:
# Put the number "5" in a box named "apples"
apples <- 5
# Put "3" in a box named "oranges"
oranges <- 3
# How many fruits?
total_fruits <- apples + oranges
Type total_fruits in the Console. R says 8!
📝 Rules:
- Use
<- to put things in boxes.
- Names CAN’T have spaces (use
total_fruits not total fruits).
Page 5: Different Types of Data
R knows different "flavors" of data:
| Data Type | Example | Purpose |
|---|
| Numeric | 3.14, -5 | Numbers |
| Character | "hello", "DNA" | Text (use quotes!) |
| Logical | TRUE, FALSE | Yes/No questions |
Try this:
gene_name <- "BRCA1"
is_mutated <- TRUE
mutations_count <- 12
Page 6: Vectors (Lists of Things)
A vector is a shopping list:
# A vector of patient ages
patient_ages <- c(25, 31, 40, 19)
# A vector of gene names
genes <- c("TP53", "EGFR", "MYC")
Get the 1st patient’s age:
patient_ages[1] # R says "25"
🔍 Fun Fact:
In R, counting starts at 1 (not 0 like in some languages).
Page 7: Making Tables (Data Frames)
A data frame is like an Excel sheet:
# Make a table of patients
patients <- data.frame(
name = c("Alice", "Bob", "Charlie"),
age = c(25, 31, 40),
has_cancer = c(FALSE, TRUE, TRUE)
)
View your table:
Type View(patients) in the Console. A spreadsheet pops up!
Page 8: Asking Questions with Logic
Ask R yes/no questions:
# Is Alice older than 30?
alice_age <- 25
alice_age > 30 # R says "FALSE"
# Is Bob's status "has_cancer"?
bob_status <- TRUE
bob_status == TRUE # "TRUE"
⚠️ Note:
= means "put this in a box".
== means "are these equal?" (like a detective).
Page 9: Basic Functions (R’s Superpowers)
Functions are magic spells:
# Spell to calculate the mean
mean(patient_ages) # (25+31+40+19)/4 = 28.75!
# Spell to count items
length(genes) # 3 genes!
Your Turn:
# Calculate the max age in patient_ages
max(patient_ages) # R says "40"
Page 10: Installing Packages (Adding New Tools)
Packages are free DLCs for R!
-
Install a package:
install.packages("tidyverse") # The ultimate bioinformatics toolkit!
-
Load it:
library(tidyverse)
💡 Pro Tip:
You only install.packages() once.
You library() every time you restart R.
Page 11: Making Simple Graphs
Let’s plot patient ages!
# Bar plot
barplot(patient_ages,
names.arg = c("Alice", "Bob", "Charlie", "Diana"),
col = "pink",
main = "Patient Ages")
A bar chart appears in the bottom-right panel!
Page 12: Reading Data from Files
Load a real DNA dataset!
-
Download a practice CSV file:
dna_data.csv (pretend link).
-
Read it into R:
dna_data <- read.csv("dna_data.csv")
View(dna_data)
🚨 Important:
Save your CSV in R’s working directory (check with getwd()).
Page 13: Filtering Data (Finding Needles in Haystacks)
Use dplyr to find cancer patients:
library(dplyr)
cancer_patients <- patients %>%
filter(has_cancer == TRUE)
Now cancer_patients shows only Bob & Charlie!
Page 14: Creating New Columns
Add a column for "age_group":
patients <- patients %>%
mutate(age_group = ifelse(age >= 30, "Old", "Young"))
Now your table has a new column!
Page 15: Basic Statistics
Calculate mean, min, max:
patients %>%
summarize(
mean_age = mean(age),
min_age = min(age),
max_age = max(age)
)
Page 16: Fancy Graphs with ggplot2
Make a publication-quality plot:
library(ggplot2)
ggplot(patients, aes(x = name, y = age, fill = age_group)) +
geom_col() +
labs(title = "Patient Ages by Group", x = "Name", y = "Age")
Voilà! A colorful bar chart.
Page 17: Fixing Errors (Debugging)
Common mistakes & fixes:
| Error | Solution |
|---|
object 'genee' not found | Check spelling! (gene ≠ genee) |
unexpected ',' | Did you forget ) or "? |
Debugging Tip:
- Break code into small pieces.
- Google the error (e.g., "R Error: object not found").
Page 18: Saving Your Work
Save your data:
write.csv(patients, "patients_updated.csv")
Save your graph:
- In Plots panel → Export → Save as Image (PNG/PDF).
Page 19: Review Cheat Sheet
Must-Know Commands:
x <- 5 # Assign variable
mean(x) # Calculate mean
c(1, 2, 3) # Create vector
data.frame(...) # Create table
read.csv("file.csv")# Load data
filter(data, ...) # Filter rows
ggplot() + geom_xxx()# Make graph
Page 20: What’s Next?
You’ve completed Phase 1! 🎉
💬 Remember:
Even experts started where you are.
Mistakes = Progress.
Congratulations! You now speak basic R. In Phase 2, we’ll use these skills to analyze real biological datasets. 🔬