RStudio Track

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.

Lesson 1

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.

Lesson 2

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_idconditiontemperature_cphenzyme_activitycell_viability
E01control377.211.296
E02control377.210.895
E03control377.111.594
E04control377.310.997
E05control377.211.096
E06control377.111.395
E07treatment397.214.193
E08treatment397.213.792
E09treatment397.114.591
E10treatment397.315.090
E11treatment397.213.992
E12treatment397.114.393
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
Lesson 3

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."

Lesson 4

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.

Lesson 5

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.
Practice

Exercises

Skill Check A

  1. What does enzyme_activity ~ condition mean in plain English?
  2. What is the mean enzyme activity for each condition?
  3. What is the treatment minus control difference?

Skill Check B

  1. Run a grouped summary for cell_viability.
  2. Why is cell viability a possible limitation?
  3. Write a one-sentence result for a poster.
Answer Key

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."