RStudio for Biology Data Analysis
This page teaches the RStudio workflow directly: build a project, read a data frame, summarize groups, run tests, make plots, and write a defensible result.
The RStudio Mental Model
RStudio helps you keep a research project organized. Think in four parts: a script where your instructions live, a console where code runs, an environment where objects appear, and files/plots where outputs are stored.
R Objects
In R, you usually create an object and then ask questions about it. The assignment operator is <-. Read it as "gets."
activity <- c(11.2, 10.8, 11.5)
mean(activity)
Teacher note: A script is better than typing everything into the console because the script becomes your methods record. If a judge asks what you did, your script should answer.
Practice Dataset: Enzyme Activity
This is the same dataset used in the Python track. The research question is: does the treatment condition increase enzyme activity compared with control?
| sample_id | condition | temperature_c | ph | enzyme_activity | cell_viability |
|---|---|---|---|---|---|
| E01 | control | 37 | 7.2 | 11.2 | 96 |
| E02 | control | 37 | 7.2 | 10.8 | 95 |
| E03 | control | 37 | 7.1 | 11.5 | 94 |
| E04 | control | 37 | 7.3 | 10.9 | 97 |
| E05 | control | 37 | 7.2 | 11.0 | 96 |
| E06 | control | 37 | 7.1 | 11.3 | 95 |
| E07 | treatment | 39 | 7.2 | 14.1 | 93 |
| E08 | treatment | 39 | 7.2 | 13.7 | 92 |
| E09 | treatment | 39 | 7.1 | 14.5 | 91 |
| E10 | treatment | 39 | 7.3 | 15.0 | 90 |
| E11 | treatment | 39 | 7.2 | 13.9 | 92 |
| E12 | treatment | 39 | 7.1 | 14.3 | 93 |
sample_id,condition,temperature_c,ph,enzyme_activity,cell_viability
E01,control,37,7.2,11.2,96
E02,control,37,7.2,10.8,95
E03,control,37,7.1,11.5,94
E04,control,37,7.3,10.9,97
E05,control,37,7.2,11.0,96
E06,control,37,7.1,11.3,95
E07,treatment,39,7.2,14.1,93
E08,treatment,39,7.2,13.7,92
E09,treatment,39,7.1,14.5,91
E10,treatment,39,7.3,15.0,90
E11,treatment,39,7.2,13.9,92
E12,treatment,39,7.1,14.3,93
Data Frames: Tables in R
An R data frame is a table. Each row is one sample. Each column is one variable. Your first job is always to inspect the data before analyzing it.
Read and Inspect
df <- read.csv("enzyme_activity.csv")
head(df)
dim(df)
names(df)
table(df$condition)
clean <- subset(df, !is.na(condition) & !is.na(enzyme_activity))
The dollar sign selects one column from a data frame. For example, df$condition means "the condition column inside df."
Grouped Summaries
A grouped summary compares each condition. Here the key comparison is mean enzyme activity in control samples versus treatment samples.
Base R Summary
aggregate(enzyme_activity ~ condition, clean, function(x) {
c(n = length(x), mean = mean(x), sd = sd(x))
})
control <- subset(clean, condition == "control")$enzyme_activity
treatment <- subset(clean, condition == "treatment")$enzyme_activity
mean(treatment) - mean(control)
The formula enzyme_activity ~ condition means: summarize enzyme activity separately for each condition.
Statistical Tests and Plots
R can run a Welch t-test directly from a formula. The test asks whether the group means are different relative to the variation within groups.
Welch t-Test
result <- t.test(enzyme_activity ~ condition, data = clean)
result
result$p.value
Simple Plot
boxplot(enzyme_activity ~ condition,
data = clean,
xlab = "Condition",
ylab = "Enzyme activity",
main = "Treatment samples show higher enzyme activity")
Interpretation template: Treatment samples had a higher mean enzyme activity than controls. A Welch t-test gave p = Y. Because treatment samples also had lower viability, the poster should mention possible stress or toxicity as a limitation.
Exercises
Skill Check A
- What does
enzyme_activity ~ conditionmean in plain English? - What is the mean enzyme activity for each condition?
- What is the treatment minus control difference?
Skill Check B
- Run a grouped summary for
cell_viability. - Why is cell viability a possible limitation?
- Write a one-sentence result for a poster.
Check Your Work
Skill Check A Answers
enzyme_activity ~ condition means "compare enzyme activity across condition groups." The control mean enzyme activity is about 11.12. The treatment mean is 14.25. The treatment group is higher by about 3.13 activity units.
t-Test Answer
R's default two-sample t.test uses Welch's method. The p-value is about 5.79e-07 for this dataset, which is strong evidence that the treatment and control groups differ in enzyme activity. The test does not prove the treatment is the only cause of the difference.
Skill Check B Answers
Mean cell viability is 95.5 for control and 91.83 for treatment. That is a limitation because the treatment condition may be changing both enzyme activity and cell health. A good poster sentence would be: "Treatment samples showed higher enzyme activity than controls, but lower treatment viability suggests the effect should be interpreted alongside possible cell stress."