Lesson · 40 min · Free
R for Biologists: Getting Started
R for Biologists: Getting Started R is a free, open-source programming language built for statistics and data visualization, and it has become one of the most important tools in modern computational biology. Packages suc
R for Biologists: Getting Started
R is a free, open-source programming language built for statistics and data visualization, and it has become one of the most important tools in modern computational biology. Packages such as DESeq2 , limma , and Seurat power everything from differential gene expression analysis to single-cell RNA-seq workflows. Unlike point-and-click spreadsheet software, R allows you to write reproducible scripts: every transformation, statistical test, and plot is recorded as code that a colleague (or your future self) can re-run exactly. For pharmacy and biotech students, R is especially valuable because biological data is rarely "clean." You will constantly encounter missing values, batch effects, and inconsistent gene identifiers. R forces you to handle these issues explicitly rather than hiding them inside manual copy-paste steps, which dramatically reduces errors in downstream analyses. R works with objects . You assign data to a named object using the assignment operator <- , and then operate on that object with functions. The most fundamental data structure you will use is the vector — an ordered collection of values of the same type (numeric, character, or logical). Vectors underlie everything else in R, including data frames, which are essentially tables where each column is a vector. Let's start with the basics: creating objects and working with vectors. Type the following into the R console (or an R script in RStudio) and run it line by line: # Create numeric vectors of gene expression measurements gene_a <- c(2.4, 3.1, 2.9, 4.0, 3.7) # expression values, sample 1-5 gene_b <- c(1.1, 0.8, 1.5, 0.9, 1.2) # Basic arithmetic on entire vectors at once (vectorization!) fold_change <- gene_a / gene_b print(fold_change) # Summary statistics mean(gene_a) sd(gene_b) log2(fold_change) # log2 fold change, a staple of expression analysis Notice that operations like division and logarithms apply to every element of the vector at once — this is called vectorization , and it is one of R's greatest strengths for biological data, where you routinely process thousands of genes simultaneously. Next, let's organize vectors into a data frame, the standard structure for experimental datasets: # Build a small expression table samples <- c("S1", "S2", "S3", "S4", "S5") treatment <- c("drug", "drug", "control", "control", "drug") expression_data <- data.frame( sample_id = samples, condition = treatment, gene_a_expr = gene_a, gene_b_expr = gene_b ) # Inspect the data frame head(expression_data) str(expression_data) # Access one column and compute group means mean(expression_data$gene_a_expr[expression_data$condition == "drug"]) mean(expression_data$gene_a_expr[expression_data$condition == "control"]) # Install and load a Bioconductor-style package (example) # install.packages("ggplot2") library(ggplot2) The $ operator extracts a column from a data frame, and logical comparisons like == "drug" let you filter rows — the foundation of subsetting real datasets such as clinical cohorts or RNA-seq count matrices. Installing packages with install.packages() (or BiocManager::install() for Bioconductor packages) extends R's capabilities, and library() loads an installed package into your current session. Key takeaways: R is the dominant language for statistical genomics; packages like DESeq2 and Seurat are industry standards in bioinformatics. Use <- to assign data to named objects; scripts make analyses reproducible. Vectors are R's core data structure, and operations are vectorized — applied to all elements at once. Data frames store tabular data; access columns with $ and filter rows with logical conditions. Extend R's functionality with install.packages() and load packages with library() . Practice exercise: Create a data frame called patients with three columns: patient_id (e.g., "P01" through "P06"), dose_mg containing six numeric values of your choice, and response containing six numeric values representing a biomarker level. Then compute the mean biomarker level for patients with dose_mg greater than 50, and store the result in an object called high_dose_mean . Print it to the console, and briefly explain in a comment why vectorized subsetting is safer than manually selecting rows in a spreadsheet.
Watch the full lesson — free
This topic is part of Computational Biomedicine: From Command Line to Single-Cell, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →