11  R Programming Fundamentals

NoteLearning Objectives

After completing this chapter, you will be able to:

  • Explain why R is well-suited for scientific computing
  • Understand R’s object-oriented nature and examine objects with class(), typeof(), and str()
  • Use Positron as an integrated development environment for R
  • Perform basic arithmetic and variable assignment in R
  • Work with vectors and understand vectorized operations
  • Use logical operators and handle floating-point comparisons correctly
  • Create and manipulate data frames, lists, and matrices
  • Use built-in functions and access help documentation including vignettes
  • Interpret common error messages and handle missing values (NA) correctly
  • Manage the working directory by working in a project folder
  • Install packages and resolve namespace conflicts
  • Create basic visualizations
  • Read and write data files
  • Clean up your R environment and graphics devices

11.1 Why R for Scientific Computing?

R is a programming language designed specifically for statistical computing and graphics (R Core Team, 2024). It is an offshoot of a language called S, developed in 1976 at Bell Laboratories by John Chambers and colleagues. R was created in 1991 by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand (hence the name “R”), and has since become one of the most widely used tools in data science, statistics, and bioinformatics.

Unlike general-purpose languages that happen to have statistics libraries, R was built from the ground up for data analysis. This design choice permeates every aspect of the language—from how it handles vectors and missing values to how it creates publication-quality graphics with minimal code.

As an interpreted language, R code is translated to machine language by the R interpreter each time it runs, as opposed to being compiled beforehand. This makes R highly interactive and excellent for exploratory data analysis. You can type a command, see the result immediately, and adjust your approach—a rapid feedback loop that’s essential for understanding data.

TipLearning to Think in R

Learning R isn’t just about memorizing syntax—it’s about learning to think about data in a new way. R encourages you to work with entire vectors and data frames at once rather than processing elements one by one. This “vectorized thinking” may feel unfamiliar at first, but it leads to more concise, readable, and efficient code.

NotePrefer Python, or need both?

This chapter teaches R, the language used for statistics and graphics throughout the book. Chapter 12 covers the same ground in Python 3, section by section, and ends with an R-to-Python translation table (Table 12.7). If you already know one language, reading the two chapters side by side is the fastest way to learn the other.

R offers several advantages for researchers:

Statistical Power. R was built by statisticians for statistics. It includes comprehensive implementations of classical and modern statistical methods.

Graphics Excellence. R produces publication-quality graphics with fine-grained control over every aspect of visualization.

Package Ecosystem. Over 20,000 packages extend R’s capabilities for virtually every analytical need, from genomics to machine learning.

Reproducibility. R scripts document your analysis completely. Combined with Quarto (which produced this book; see Chapter 16), you can create reproducible documents that integrate code, results, and narrative.

Active Community. A large, welcoming community provides abundant resources, tutorials, and support.

Cost. R is free and open-source, removing financial barriers to sophisticated analysis.

The Console vs. Scripts

When you first start using R, you’ll interact with it through the console—a command-line interface where you type commands and see results immediately. This is perfect for exploration: trying things out, checking your data, and learning how functions work.

But as your analyses become more complex, you’ll want to save your work in scripts—text files (ending in .R) that contain a sequence of commands. Scripts are the foundation of reproducible research. When you run a script, R executes each command in order, exactly as you wrote it. This means:

  1. You can reproduce your analysis by running the script again
  2. Others can verify your work by reading and running your code
  3. You can modify and extend your analysis without starting from scratch
  4. Your future self will understand what you did and why

The workflow looks like this: explore in the console to understand your data, then move your successful commands into a script. Your script becomes the official record of your analysis—clean, organized, and executable.

A good script is self-contained: it holds every command needed to reproduce the analysis from a fresh R session, in order. A conventional layout is a comment header, then package loading, then data import, then the analysis, with comments (anything after #) explaining why rather than restating what the code does:

# Analysis of hydrogel mechanical properties
# Author: Your Name
# Date: 2026-10-01

# 1. Load packages ---------------------------------------------------------
library(tidyverse)

# 2. Read data -------------------------------------------------------------
hydrogel <- read.csv("data/hydrogel_data.csv")

# 3. Summarize -------------------------------------------------------------
summary(hydrogel)

# 4. Visualize -------------------------------------------------------------
ggplot(hydrogel, aes(x = concentration, y = compression)) +
  geom_boxplot()

In Positron or VS Code, create a script with File → New File (choose R), then run the current line or selection with Ctrl+Enter (Cmd+Enter on a Mac). Comment lines ending in four or more dashes, as above, become collapsible sections in the editor’s outline. Quarto documents (Chapter 16) extend the same idea by interleaving code chunks with formatted narrative.

A script can also be run from the shell without opening an IDE at all, which is how analyses run on Talapas (Chapter 20). The same small program looks like this in R and in Python; the shell side of running scripts is covered in Section 10.10.

#!/usr/bin/env Rscript
# summarize.R - mean response per treatment
args <- commandArgs(trailingOnly = TRUE)
if (length(args) < 1) stop("Usage: Rscript summarize.R <file.csv>")

experiment <- read.csv(args[1])
means <- tapply(experiment$response, experiment$treatment, mean)
print(round(means, 2))
Rscript summarize.R experiment.csv
#!/usr/bin/env python3
# summarize.py - mean response per treatment
import sys
import pandas as pd

if len(sys.argv) < 2:
    sys.exit("Usage: python3 summarize.py <file.csv>")

experiment = pd.read_csv(sys.argv[1])
means = experiment.groupby("treatment")["response"].mean()
print(means.round(2))
python3 summarize.py experiment.csv

A Fuller Example: FASTA Statistics in R and Python

The bash script fasta_stats.sh in Section 10.12 counts the sequences in a FASTA file, totals their length, and reports the N count, GC content, and number of ORF-like stretches. Counting and arithmetic are exactly where bash gets awkward, and the same program is shorter and clearer in either R or Python. Both versions below take the file name as a command-line argument, check that the file exists, and exit with a non-zero status on failure, so a shell pipeline or a Slurm job (Chapter 20) can tell whether they succeeded—just like exit 1 in bash.

#!/usr/bin/env Rscript
# fasta_stats.R - basic statistics for a FASTA file
args <- commandArgs(trailingOnly = TRUE)
if (length(args) != 1) {
  cat("Usage: Rscript fasta_stats.R sequences.fasta\n")
  quit(status = 1)
}
input <- args[1]
if (!file.exists(input)) {
  cat("Error: File", input, "not found!\n")
  quit(status = 1)
}

lines   <- readLines(input)
headers <- grepl("^>", lines)                # TRUE for header lines
seq     <- paste(lines[!headers], collapse = "")

cat("=== FASTA Statistics for", input, "===\n")
cat("Number of sequences:", sum(headers), "\n")
cat("Total length:", nchar(seq), "bp\n")
cat("Average length:", nchar(seq) / sum(headers), "bp\n")
n_count <- sum(strsplit(seq, "")[[1]] %in% c("N", "n"))
cat("N bases:", n_count, "\n")
gc <- sum(strsplit(toupper(seq), "")[[1]] %in% c("G", "C"))
cat("GC content:", round(100 * gc / nchar(seq), 1), "%\n")
orfs <- regmatches(seq, gregexpr("ATG([ACGT]{3}){20,}(TAA|TAG|TGA)", seq))[[1]]
cat("ORF-like stretches:", length(orfs), "\n")
Rscript fasta_stats.R sequences.fasta
#!/usr/bin/env python3
# fasta_stats.py - basic statistics for a FASTA file
import re
import sys
from pathlib import Path

if len(sys.argv) != 2:                       # sys.argv[0] is the script name
    print("Usage: python3 fasta_stats.py sequences.fasta")
    sys.exit(1)
input_file = Path(sys.argv[1])
if not input_file.is_file():
    print(f"Error: File {input_file} not found!")
    sys.exit(1)

lines   = input_file.read_text().splitlines()
headers = [line for line in lines if line.startswith(">")]
seq     = "".join(line for line in lines if not line.startswith(">"))

print(f"=== FASTA Statistics for {input_file} ===")
print(f"Number of sequences: {len(headers)}")
print(f"Total length: {len(seq)} bp")
print(f"Average length: {len(seq) / len(headers):.1f} bp")
n_count = seq.count("N") + seq.count("n")
print(f"N bases: {n_count}")
gc = seq.upper().count("G") + seq.upper().count("C")
print(f"GC content: {100 * gc / len(seq):.1f}%")
orfs = re.findall(r"ATG(?:[ACGT]{3}){20,}(?:TAA|TAG|TGA)", seq)
print(f"ORF-like stretches: {len(orfs)}")
python3 fasta_stats.py sequences.fasta

Read the R version as a preview of the rest of this chapter: readLines() returns a character vector with one element per line, grepl("^>", lines) applies a regular expression (Chapter 9) to every element at once and returns a logical vector, lines[!headers] is logical indexing (Section 11.6.5), and sum(headers) counts the TRUE values. The Python version does the same work with list comprehensions and string methods (Chapter 12); sys.argv and the more capable argparse are covered in Section 12.14. Where the bash version chained grep, tr, and wc through pipes, each language here does the counting itself.

Figure 11.1: One program, three vocabularies: every version of the FASTA statistics script passes through the same four stages—read arguments, validate input, count and compute, report—and only the names change between bash, R, and Python.

11.2 Positron: Your R Environment

While R can run from the command line, most users work in an integrated development environment (IDE). In this course that is Positron (Section 2.5), which is built on the same core as VS Code and adds the panes a data scientist needs.

The Positron Interface

Positron organizes your workspace into a few key regions:

Editor (center). Where you write and edit scripts and Quarto documents. Code here isn’t executed until you explicitly run it.

Console (bottom). The interactive R (or Python) session. Commands typed here execute immediately, and lines sent from the editor appear here with their output.

Variables (right). Shows the objects you’ve created in the current session, with their type and size; click a data frame to open it in the Data Explorer.

Plots, Help, and Explorer (right and left). The plot viewer, help documentation, and the file browser for the folder you have open.

NoteR is not the IDE

R is the programming language; Positron (or VS Code) is the integrated development environment that lets you write and run R (and Python) comfortably. R must be installed first, and the IDE then finds it. If you ever need to, you can run R directly from the shell by typing R.

Tip

Learn keyboard shortcuts! Ctrl+Enter (Windows/Linux) or Cmd+Enter (Mac) runs the current line or selection in the console. The essential editor shortcuts are collected in Appendix A.

Project Folders and the Working Directory

Every R session has a working directory: the folder that relative paths such as "data/results.csv" are resolved against, exactly as in the shell (Chapter 5). You can inspect and change it from R:

getwd()                       # Where am I?
setwd("/path/to/my/project")  # Move (avoid in scripts; see below)
list.files()                  # What files are here?
list.files("data")            # ...and in a subfolder

Working in a project folder removes the need for setwd() altogether:

  1. Keep each project in its own directory (see Section 16.15 for a typical layout)
  2. In Positron, use File → Open Folder to open that directory, not a single file
  3. The R console starts with the working directory set to that folder

Because the working directory is the project folder, paths in your scripts can be relative to the project root and the code runs unchanged on a collaborator’s computer. A setwd("/Users/yourname/...") line at the top of a script is a red flag: it will fail for everyone but you.

11.3 Understanding R: Everything is an Object

Before diving into R syntax, it helps to understand R’s fundamental design philosophy: everything in R is an object. This object-oriented approach means that every piece of data you work with—numbers, text, datasets, even functions themselves—is stored as an object with specific properties.

# Numbers are objects
x <- 42
class(x)
#> [1] "numeric"
typeof(x)
#> [1] "double"

# Text strings are objects
name <- "Gene Expression"
class(name)
#> [1] "character"
typeof(name)
#> [1] "character"

# Even functions are objects!
class(mean)
#> [1] "function"
typeof(mean)
#> [1] "closure"

# And data frames are objects
samples_df <- data.frame(a = 1:3, b = c("x", "y", "z"))
class(samples_df)
#> [1] "data.frame"
typeof(samples_df)
#> [1] "list"

Every object has:

  • A class: Determines how the object behaves with functions (what class() returns)
  • A type: The underlying storage mode (what typeof() returns)
  • Attributes: Optional metadata like names, dimensions, or custom properties

The str() function provides a compact display of an object’s structure:

# Examine structure of different objects
str(x)
#>  num 42
str(name)
#>  chr "Gene Expression"
str(samples_df)
#> 'data.frame':    3 obs. of  2 variables:
#>  $ a: int  1 2 3
#>  $ b: chr  "x" "y" "z"

Understanding this object-oriented nature helps you:

  1. Debug errors: When functions fail, check what class of object you’re passing
  2. Use generic functions: Functions like print(), summary(), and plot() behave differently based on object class
  3. Extend R: You can create custom classes with specialized behaviors

11.4 R Basics: Arithmetic and Variables

R as a Calculator

The simplest use of R is arithmetic:

# Basic operations
4 * 4
#> [1] 16

# Order of operations applies
(4 + 3 * 2^2)
#> [1] 16

# More complex expressions
sqrt(144) + log(100)
#> [1] 16.60517

# Integer division and modulo
17 %/% 5    # Integer division: how many times 5 goes into 17
#> [1] 3
17 %% 5     # Modulo: remainder after division
#> [1] 2

R follows standard mathematical order of operations (PEMDAS). The modulo operator (%%) is particularly useful for tasks like determining if a number is even or odd, cycling through indices, or extracting digits from numbers:

# Check if numbers are even (remainder 0 when divided by 2)
numbers <- 1:10
numbers %% 2 == 0
#>  [1] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE  TRUE

# Extract the last digit of a number
12345 %% 10
#> [1] 5

Creating Variables

Variables store values for later use. In R, the assignment operator is <-:

# Assign values to variables
x <- 2
y <- 5

# Use variables in calculations
x * 3
#> [1] 6
y + x
#> [1] 7

# Store results in new variables
z <- x * y
z
#> [1] 10

Notice that an assignment itself prints nothing: z <- x * y silently stores the value. To see what a variable holds, type its name alone or call print():

z
#> [1] 10
print(z)
#> [1] 10
Note

You can also use = for assignment in R, but <- is the traditional and preferred style. It makes assignment visually distinct from function arguments, which use =.

Variable Naming Rules

  • Names must start with a letter
  • Can contain letters, numbers, periods, and underscores
  • Names cannot contain operators (-, *, +, spaces) because R would try to evaluate them
  • R is case-sensitive: Gene, gene, and GENE are three different variables
  • Use descriptive names: sample_count is better than sc
  • Prefer underscores to periods: sample.count is legal R, but the period has a different meaning in Python (Chapter 12), and snake_case names move between the two languages without surprises
# Valid names
gene_count <- 100
Sample.1 <- "control"
myData2 <- 42

# Invalid (would cause errors)
# 2samples <- 5      # Can't start with number
# 3*y <- 3           # Can't include operators
# my-data <- 10      # Hyphens not allowed (R reads this as my minus data)

Reserved Words

R has reserved words (Table 11.1) that cannot be used as variable names because they have special meaning in the language:

Table 11.1: R reserved words
Reserved Words Description
if, else Conditional statements
for, while, repeat Loop constructs
function Function definition
in Used in for loops
next, break Loop control
TRUE, FALSE Logical constants
NULL Null object
Inf, NaN, NA Special numeric values

R also has semi-reserved words—names of built-in constants and functions that you can overwrite but generally shouldn’t:

# These work but are dangerous:
T <- 5       # Overwrites TRUE abbreviation
F <- 10      # Overwrites FALSE abbreviation
c <- "text"  # Shadows the c() function
mean <- 42   # Shadows the mean() function

# If you accidentally overwrite something, use rm() to remove it
rm(c)        # Removes your 'c' variable, restoring access to c()
WarningAvoid Common Name Collisions

Avoid using T and F as variable names—they are common abbreviations for TRUE and FALSE. Similarly, never name variables c, t, mean, sum, data, or df: these are all existing R functions (df() is the F-distribution density and data() loads built-in datasets), and shadowing them leads to confusing errors such as object of type 'closure' is not subsettable, which means “you tried to index a function.” Names like experiment, counts, or samples_df are safer and more descriptive.

11.5 Key R Terminology

Before diving deeper into R, let’s establish some fundamental terminology that you’ll encounter throughout your R programming journey (Figure 11.2):

Figure 11.2: Core R concepts: Objects, Vectors, Functions, Parameters, and Arguments are the building blocks of R programming.

Understanding R’s operators is equally important. Figure 11.3 shows the main operators you’ll use, listed in order of precedence (operations higher in the table are evaluated first):

Figure 11.3: R operators reference: This table shows common R operators for indexing, arithmetic, comparison, logical operations, and assignment.
Tip

Note that R uses <- for assignment (right to left), while = is typically used for argument assignment within function calls. Both work for variable assignment, but <- is the conventional R style.

11.6 Vectors: R’s Fundamental Data Structure

R thinks in terms of vectors—ordered collections of values of the same type. Even a single number is a vector of length 1. Because all elements must share a type, mixing types in c() silently converts the whole vector to the most flexible one: c(1, "a", TRUE) becomes three character strings, and c(1, TRUE) becomes 1 1.

Creating Vectors

The c() function (for “combine” or “concatenate”) creates vectors:

# Numeric vector
measurements <- c(2, 3, 4, 2, 1, 2, 4, 5, 10, 8, 9)
measurements
#>  [1]  2  3  4  2  1  2  4  5 10  8  9

# Character vector
samples <- c("control", "treatment_A", "treatment_B")
samples
#> [1] "control"     "treatment_A" "treatment_B"

# Logical vector
passed <- c(TRUE, FALSE, TRUE, TRUE, FALSE)
passed
#> [1]  TRUE FALSE  TRUE  TRUE FALSE

A few functions tell you about any vector without printing all of it—essential once vectors have thousands of elements:

length(measurements)   # How many elements?
#> [1] 11
class(measurements)    # What type?
#> [1] "numeric"
head(measurements, 3)  # First few
#> [1] 2 3 4
tail(measurements, 3)  # Last few
#> [1] 10  8  9

The numbers in square brackets at the start of each output line, such as [1], are not part of the data: they give the index of the first element on that line, which helps you find your place in long output.

Sequence Generation

For regular sequences, use shortcuts:

# Integer sequence
1:10
#>  [1]  1  2  3  4  5  6  7  8  9 10

# Sequence with step
seq(0, 10, by = 0.5)
#>  [1]  0.0  0.5  1.0  1.5  2.0  2.5  3.0  3.5  4.0  4.5  5.0  5.5  6.0  6.5  7.0
#> [16]  7.5  8.0  8.5  9.0  9.5 10.0

# Specified length
seq(0, 1, length.out = 5)
#> [1] 0.00 0.25 0.50 0.75 1.00

# Repeated values
rep("A", 5)
#> [1] "A" "A" "A" "A" "A"
rep(c(1, 2), times = 3)
#> [1] 1 2 1 2 1 2
rep(c(1, 2), each = 3)
#> [1] 1 1 1 2 2 2

Sorting Vectors

The sort() function returns a sorted vector:

# Sort in ascending order (default)
x <- c(5, 2, 8, 1, 9, 3)
sort(x)
#> [1] 1 2 3 5 8 9

# Sort in descending order
sort(x, decreasing = TRUE)
#> [1] 9 8 5 3 2 1

# Sorting character vectors (alphabetical)
genes <- c("BRCA1", "TP53", "EGFR", "KRAS")
sort(genes)
#> [1] "BRCA1" "EGFR"  "KRAS"  "TP53"

To get the indices that would sort a vector (useful for reordering related data), use order():

x <- c(5, 2, 8, 1, 9, 3)
order(x)  # Indices: 4th element is smallest, 2nd is next, etc.
#> [1] 4 2 6 1 3 5

# Use order() to sort one vector by another
names <- c("Sample_A", "Sample_B", "Sample_C")
values <- c(30, 10, 20)
names[order(values)]  # Names sorted by values
#> [1] "Sample_B" "Sample_C" "Sample_A"

Vectorized Operations

R’s power comes from vectorized operations—operations that apply to entire vectors at once:

x <- c(2, 3, 4, 2, 1, 2, 4, 5, 10, 8, 9)

# Operations apply to each element
x * 2
#>  [1]  4  6  8  4  2  4  8 10 20 16 18
x^2
#>  [1]   4   9  16   4   1   4  16  25 100  64  81
log(x)
#>  [1] 0.6931472 1.0986123 1.3862944 0.6931472 0.0000000 0.6931472 1.3862944
#>  [8] 1.6094379 2.3025851 2.0794415 2.1972246
sqrt(x)
#>  [1] 1.414214 1.732051 2.000000 1.414214 1.000000 1.414214 2.000000 2.236068
#>  [9] 3.162278 2.828427 3.000000

# Operations between vectors (element-wise)
y <- 1:11
x + y
#>  [1]  3  5  7  6  6  8 11 13 19 18 20
x * y
#>  [1]  2  6 12  8  5 12 28 40 90 80 99

This is much faster and cleaner than writing loops!

Vector Indexing

Access specific elements using square brackets:

measurements <- c(2, 3, 4, 2, 1, 2, 4, 5, 10, 8, 9)

# Single element (1-indexed!)
measurements[1]
#> [1] 2
measurements[5]
#> [1] 1

# Multiple elements
measurements[c(1, 3, 5)]
#> [1] 2 4 1

# Range
measurements[2:5]
#> [1] 3 4 2 1

# Negative indices exclude
measurements[-1]          # All but first
#>  [1]  3  4  2  1  2  4  5 10  8  9
measurements[-(1:3)]      # All but first three
#> [1]  2  1  2  4  5 10  8  9

# Logical indexing
measurements[measurements > 5]
#> [1] 10  8  9

Figure 11.4 traces that last expression step by step.

Figure 11.4: Logical indexing step by step: the comparison measurements > 5 produces a logical vector the same length as measurements, and placing it inside [ ] keeps only the elements where it is TRUE. NumPy arrays use the identical idiom.
Important

R uses 1-based indexing! The first element is [1], not [0] as in Python and most other languages (see Section 12.16).

11.7 Data Types in R

R has several fundamental data types:

Table 11.2: R data types. The abbreviations are what tibbles (Section 14.5) and glimpse() print under each column name.
Type Tibble abbreviation Description Example
numeric (double) <dbl> Real numbers (default) 3.14, 42
integer <int> Whole numbers 1L, 100L
character <chr> Text strings "hello", "gene1"
logical <lgl> Boolean values TRUE, FALSE, NA
factor <fct> Categorical variables Treatment levels
Date, POSIXct <date>, <dttm> Dates and date-times Sys.Date(), Sys.time()

Numbers are doubles by default: 42 is numeric, and you must write 42L to get an integer. Integer and double vectors together are called numeric vectors.

Figure 11.5: The three core atomic vector types—logical, numeric (integer and double), and character—and the augmented types built on top of them: factors are built on integer vectors, while dates and date-times are built on doubles.
# Check types with class()
class(3.14)
#> [1] "numeric"
class("hello")
#> [1] "character"
class(TRUE)
#> [1] "logical"

# Type coercion
as.numeric("42")
#> [1] 42
as.character(123)
#> [1] "123"
as.logical(0)    # FALSE
#> [1] FALSE
as.logical(1)    # TRUE
#> [1] TRUE
as.numeric("hello")  # NA, with a warning
#> [1] NA
WarningCoercing Factors to Numbers

as.numeric() on a factor returns the internal integer codes of the levels, not the numbers you can see:

doses <- factor(c("10", "50", "10", "100"))
as.numeric(doses)                # Level codes: 1, 3, 1, 2
#> [1] 1 3 1 2
as.numeric(as.character(doses))  # The actual values
#> [1]  10  50  10 100

Convert through as.character() first. This trap most often appears when a numeric column was read from a file as text or factor because of a stray non-numeric entry.

Factors

Factors are special vectors for representing categorical data with a fixed set of possible values called levels. They’re essential for statistical modeling and data analysis in R:

# Create a factor from a character vector
treatment <- c("control", "drug_A", "drug_A", "control", "drug_B", "drug_B")
treatment_factor <- as.factor(treatment)
treatment_factor
#> [1] control drug_A  drug_A  control drug_B  drug_B 
#> Levels: control drug_A drug_B

# Check the levels
levels(treatment_factor)
#> [1] "control" "drug_A"  "drug_B"
Note

Factor levels are stored alphabetically by default, regardless of the order they appear in your data. This affects how results are displayed and can impact statistical analyses (e.g., the reference level in regression models).

To specify a custom level order, use the levels argument:

# Custom level order
treatment_ordered <- factor(treatment,
                            levels = c("control", "drug_A", "drug_B"))
levels(treatment_ordered)
#> [1] "control" "drug_A"  "drug_B"

# This matters for plotting and statistical models
# where "control" should be the reference level

Factors are memory-efficient because they store unique levels once and use integer indices to reference them. Use str() to see this internal representation:

str(treatment_factor)
#>  Factor w/ 3 levels "control","drug_A",..: 1 2 2 1 3 3

Special Values

R has special values for missing data and undefined results (Table 11.3):

# Missing value
NA
#> [1] NA

# Not a number (undefined)
0/0
#> [1] NaN
log(-1)
#> [1] NaN

# Infinity
1/0
#> [1] Inf
-1/0
#> [1] -Inf
Table 11.3: Special values in R
Value Meaning Produced by Test with
NA Not Available (missing data) Blank cells in a file, failed coercion is.na()
NaN Not a Number (undefined math) 0/0, sqrt(-1) is.nan() (also is.na())
Inf, -Inf Positive / negative infinity 1/0, -1/0 is.infinite(), is.finite()
NULL The empty object Missing list elements, functions that return nothing is.null()

Handling NA values is a common task in data analysis. Any blank entry in a data file becomes NA when read into R, and NA is contagious: most arithmetic and summary functions return NA if any input is missing, so that a missing value is never silently ignored.

num <- c(0, 1, 2, NA, 4)

mean(num)               # NA: one missing value poisons the result
#> [1] NA
mean(num, na.rm = TRUE) # Most summary functions accept na.rm = TRUE
#> [1] 1.75

is.na(num)              # Which elements are missing?
#> [1] FALSE FALSE FALSE  TRUE FALSE
sum(is.na(num))         # How many?
#> [1] 1
num[!is.na(num)]        # Keep only the non-missing values
#> [1] 0 1 2 4
ImportantNever test for missing values with ==

NA means “unknown,” so R cannot say whether an unknown value equals anything—num == NA returns NA for every element rather than TRUE/FALSE. Always use is.na(). Note also that NA is a logical constant (class(NA) is "logical") and a reserved word: it is distinct from the string "NA", from 0, and from an empty string.

Logical Operators and Comparisons

R provides a rich set of operators for logical comparisons and Boolean operations:

# Comparison operators
5 > 3     # Greater than
#> [1] TRUE
5 < 3     # Less than
#> [1] FALSE
5 >= 5    # Greater than or equal
#> [1] TRUE
5 <= 5    # Less than or equal
#> [1] TRUE
5 == 5    # Equal (note: two equals signs!)
#> [1] TRUE
5 != 3    # Not equal
#> [1] TRUE

# Logical operators
TRUE & FALSE   # AND
#> [1] FALSE
TRUE | FALSE   # OR
#> [1] TRUE
!TRUE          # NOT
#> [1] FALSE

# Vectorized logical operations
x <- c(1, 5, 10, 15, 20)
x > 5 & x < 18   # Both conditions must be true
#> [1] FALSE FALSE  TRUE  TRUE FALSE
x < 5 | x > 15   # Either condition can be true
#> [1]  TRUE FALSE FALSE FALSE  TRUE

The %in% operator is particularly useful for checking membership in a set:

# Check if values are in a set
genes <- c("BRCA1", "TP53", "EGFR", "KRAS")
target_genes <- c("TP53", "EGFR")

genes %in% target_genes
#> [1] FALSE  TRUE  TRUE FALSE

# Useful for subsetting
genes[genes %in% target_genes]
#> [1] "TP53" "EGFR"

Finding Indices with which()

The which() function returns the indices where a logical condition is TRUE—useful when you need positions rather than values:

x <- c(10, 5, 20, 15, 8, 25)

# Find indices where condition is TRUE
which(x > 12)
#> [1] 3 4 6

# Get the values at those positions
x[which(x > 12)]
#> [1] 20 15 25

# Find the index of the maximum value
which.max(x)
#> [1] 6

# Find the index of the minimum value
which.min(x)
#> [1] 2

# Find indices of specific values
which(x == 15)
#> [1] 4

# Practical example: find which samples exceed a threshold
expression_levels <- c(2.5, 8.1, 4.2, 12.3, 6.7, 15.2)
high_expression <- which(expression_levels > 10)
high_expression  # Indices 4 and 6
#> [1] 4 6
Tip

While which() is useful, you often don’t need it—logical vectors work directly for subsetting:

# These are equivalent:
x[which(x > 12)]
#> [1] 20 15 25
x[x > 12]
#> [1] 20 15 25

Use which() when you specifically need the index positions (e.g., to report which samples, to use in loops, or for which.max()/which.min()).

ImportantOperator Precedence

R evaluates operators in a specific order. From highest to lowest precedence:

  1. ^ (exponentiation)
  2. - (unary minus, for negation)
  3. : (sequence)
  4. %any% (special operators like %%, %/%, %in%)
  5. *, / (multiplication, division)
  6. +, - (addition, subtraction)
  7. <, >, <=, >=, ==, != (comparisons)
  8. ! (logical NOT)
  9. &, && (logical AND)
  10. |, || (logical OR)
  11. <-, = (assignment)

Use parentheses to make your intentions clear and avoid unexpected results.

Floating-Point Precision

A common source of confusion involves floating-point arithmetic. Computers represent decimal numbers with limited precision, which can lead to unexpected results:

# This seems wrong, but is due to floating-point representation
0.1 + 0.2 == 0.3
#> [1] FALSE

# See the actual values
print(0.1 + 0.2, digits = 20)
#> [1] 0.30000000000000004441
print(0.3, digits = 20)
#> [1] 0.2999999999999999889

Instead of using == for floating-point comparisons, use all.equal():

# Safe comparison for floating-point numbers
all.equal(0.1 + 0.2, 0.3)
#> [1] TRUE

# Returns TRUE if values are nearly equal (within tolerance)
isTRUE(all.equal(0.1 + 0.2, 0.3))
#> [1] TRUE

# You can also specify a tolerance
all.equal(1.0, 1.0001, tolerance = 0.001)
#> [1] TRUE
Warning

Always use all.equal() or check if the difference is within an acceptable tolerance when comparing floating-point numbers. Never rely on == for exact equality of decimal calculations.

Using dplyr::near() for Comparisons

The tidyverse provides dplyr::near() as a convenient alternative for checking near-equality, especially when working with data frames:

library(dplyr)

# near() returns TRUE for values within a small tolerance
near(0.1 + 0.2, 0.3)
#> [1] TRUE

# Works well in filter() and mutate()
values <- tibble(x = c(0.1 + 0.2, 0.5, 0.3))
values |> filter(near(x, 0.3))
x
0.3
0.3

# You can specify a custom tolerance
near(1.0, 1.001, tol = 0.01)
#> [1] TRUE

Use near() when filtering or comparing numeric columns in data frames, and all.equal() when you need detailed comparison information.

11.8 Functions

Functions perform operations on inputs and return outputs. R has thousands of built-in functions.

Using Functions

x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

# Basic statistical functions
mean(x)
#> [1] 5.5
median(x)
#> [1] 5.5
sd(x)        # Standard deviation
#> [1] 3.02765
var(x)       # Variance
#> [1] 9.166667
sum(x)
#> [1] 55
length(x)
#> [1] 10
min(x)
#> [1] 1
max(x)
#> [1] 10
range(x)     # Returns min and max
#> [1]  1 10

Function Arguments

Functions take arguments that modify their behavior:

# Round to 2 decimal places
round(3.14159, digits = 2)
#> [1] 3.14

# Sample with replacement
sample(1:10, size = 5, replace = TRUE)
#> [1] 4 2 9 9 1

# Mean with NA handling
values <- c(1, 2, NA, 4, 5)
mean(values)              # Returns NA
#> [1] NA
mean(values, na.rm = TRUE)  # Removes NA first
#> [1] 3

Argument Order and Naming

R functions expect arguments in a specific order. When you don’t name arguments, R matches them by position:

# These are equivalent - arguments matched by position
set.seed(42)
x1 <- rnorm(5, 0, 10)  # n, mean, sd
x1
#> [1] 13.709584 -5.646982  3.631284  6.328626  4.042683

# Named arguments can be in any order
set.seed(42)
x2 <- rnorm(sd = 10, n = 5, mean = 0)
x2
#> [1] 13.709584 -5.646982  3.631284  6.328626  4.042683
Warning

Relying on position order can lead to subtle bugs. If you accidentally swap arguments, you may get unexpected results without any error message:

# Oops! We meant 1000 samples with mean=0 and sd=10
# But we got 10 samples with mean=1000 and sd=0
wrong <- rnorm(10, 1000, 0)
wrong  # All identical values!
#>  [1] 1000 1000 1000 1000 1000 1000 1000 1000 1000 1000

When in doubt, name your arguments explicitly for clarity and safety.

Nesting Functions

One powerful feature of R is the ability to nest function calls—using the output of one function as the input to another without creating intermediate variables:

# Without nesting: create intermediate objects
z <- c(10, 20, 30)
result1 <- mean(z)
result1
#> [1] 20

# With nesting: more concise
result2 <- mean(c(10, 20, 30))
result2
#> [1] 20

# More complex nesting
# Calculate the mean of the square roots of 1 through 10
mean(sqrt(1:10))
#> [1] 2.246828

# Nested functions are evaluated from inside out
round(mean(sqrt(c(4, 9, 16, 25))), digits = 2)
#> [1] 3.5

Nesting makes code more compact, but deeply nested expressions can become hard to read. As a rule of thumb, if you find yourself nesting more than 2-3 functions, consider breaking the expression into steps or using the pipe operator (covered in Section 14.2.1).

Getting Help

R has excellent built-in documentation:

# Get help on a function
?mean
help(mean)

# Search help
help.search("correlation")
??correlation

# See function arguments
args(mean)

# See examples
example(mean)

# Find functions containing a string in their name
apropos("mean")  # Returns: "colMeans", "mean", "mean.Date", etc.

# Find functions by partial name matching
apropos("cor")   # Returns all functions containing "cor"

Vignettes and Demos

Beyond function-level help, packages often include vignettes—comprehensive tutorials that demonstrate how to use the package:

# List all available vignettes
vignette()

# List vignettes for a specific package
vignette(package = "dplyr")

# Open a specific vignette
vignette("dplyr")

# Browse vignettes in your browser
browseVignettes("ggplot2")

Some packages also include demos—interactive demonstrations of functionality:

# List all available demos
demo()

# Run a specific demo
demo(graphics)    # Base R graphics demonstration
Tip

Vignettes are often the best place to start when learning a new package. They provide context and workflows that function documentation alone cannot convey.

The help page structure:

  • Description: What the function does
  • Usage: Function syntax
  • Arguments: What inputs it accepts
  • Value: What it returns
  • Examples: Working code examples

Reading Error Messages

R’s error messages are terse but informative once you know the common ones (Table 11.4). Read them from the bottom up: the last line usually says what went wrong, and the lines above say where.

Table 11.4: Common R error messages
Error message Meaning Fix
object 'x' not found No variable with that name exists Check spelling and case; run the line that creates it
could not find function "read_csv" The package that provides it is not loaded library() the package, or check spelling
unexpected ')' or unexpected symbol Mismatched brackets or quotes, or a missing comma Count opening and closing brackets; look at the line before
non-numeric argument to binary operator Arithmetic on text (or a factor) Check the object with class(); coerce with as.numeric()
object of type 'closure' is not subsettable You indexed a function, usually because a variable was never created and its name matches a function (df, data) Run the line that creates the object; rename it
argument "x" is missing, with no default A required argument was not supplied Check args(fun) or ?fun
cannot open file 'data.csv': No such file Wrong path or working directory getwd(), list.files(); use a project and relative paths
+ prompt in the console instead of > R is waiting for you to finish an expression Press Esc and fix the unbalanced bracket or quote
TipAsking for Help

When the help page and a web search of the exact error text do not solve the problem, ask a person—but make it easy to help you. Provide a minimal reproducible example: the smallest piece of code that shows the problem, including a few rows of data (or a built-in dataset like iris), the exact error message, and what you expected instead. The reprex package formats such an example for pasting into Stack Overflow, a course forum, or an issue tracker. Preparing the example often reveals the bug by itself.

11.9 Installing and Loading Packages

Base R includes dozens of useful functions, but as you become a more advanced R user, you’ll need functions for specialized analyses. Fortunately, thousands of additional functions are distributed in the form of R packages.

Installing Packages

Packages from the Comprehensive R Archive Network (CRAN) are easy to install:

# Install a package from CRAN
install.packages("dplyr")

# Install multiple packages at once
install.packages(c("ggplot2", "readr", "tidyr"))
Note

Package names are case-sensitive and must be in quotation marks when installing. You only need to install a package once on your system (and again after upgrading R). Positron offers the same thing through the Command Palette (Cmd/Ctrl-Shift-P → R: Install Packages); installed.packages() lists what is installed. Bioconductor packages are installed with BiocManager::install() instead (Section 2.3).

Loading Packages

To use functions from an installed package, you must load it into your current session:

# Load a package
library(dplyr)

# Check if a package is installed
"dplyr" %in% rownames(installed.packages())

Unlike installation, you need to call library() every time you start a new R session. Note that you don’t need quotation marks with library().

Tip

It’s good practice to load all required packages at the beginning of your script. This makes dependencies clear and helps others reproduce your work.

Namespace Conflicts

When you load multiple packages, function names can collide. The most recently loaded package “wins,” masking functions from earlier packages:

# Loading dplyr after another package
library(MASS)
library(dplyr)
# Warning: The following object is masked from 'package:MASS': select

When conflicts occur, you have several options:

1. Use the package prefix (recommended)

# Explicitly specify which package's function to use
dplyr::select(experiment, column1, column2)
MASS::select(object)

# This works even without loading the package
stats::filter(x, method = "convolution")

2. Control loading order

Load packages with conflicting names in the order that gives you the default behavior you want.

3. Use the conflicted package

# Install and load conflicted
install.packages("conflicted")
library(conflicted)

# Now conflicts cause errors instead of silent masking
# You must explicitly choose which function to use:
conflict_prefer("filter", "dplyr")
conflict_prefer("select", "dplyr")
Warning

The filter() and lag() functions from dplyr commonly conflict with base R’s stats::filter() and stats::lag(). If your filtering code suddenly stops working, namespace conflicts are often the culprit.

11.10 Data Frames

Data frames are R’s workhorse for tabular data—think spreadsheets or database tables. Each column is a vector, and columns can have different types.

Creating Data Frames

# Create vectors
sample_id <- c("S1", "S2", "S3", "S4", "S5")
treatment <- c("control", "drug_A", "drug_A", "drug_B", "drug_B")
concentration <- c(0, 10, 20, 10, 20)
response <- c(1.2, 3.4, 5.6, 2.8, 4.1)

# Combine into data frame
experiment <- data.frame(
  sample_id = sample_id,
  treatment = treatment,
  concentration = concentration,
  response = response
)

experiment
sample_id treatment concentration response
S1 control 0 1.2
S2 drug_A 10 3.4
S3 drug_A 20 5.6
S4 drug_B 10 2.8
S5 drug_B 20 4.1

All columns must have the same length, and every column holds a single type—a numeric column cannot contain a stray "n/a" without turning the whole column into text (see Section 11.7). Rows can optionally carry names, which appear in printed output and are useful for labelling samples:

labelled <- experiment
rownames(labelled) <- paste0("Sample_", 1:5)
labelled
sample_id treatment concentration response
Sample_1 S1 control 0 1.2
Sample_2 S2 drug_A 10 3.4
Sample_3 S3 drug_A 20 5.6
Sample_4 S4 drug_B 10 2.8
Sample_5 S5 drug_B 20 4.1
Note

Row names are a base R convenience. The tidyverse’s tibbles (Section 14.5) drop them, on the principle that a sample identifier is data and belongs in an ordinary column such as sample_id; tibble::rownames_to_column() makes the conversion. In this book we keep identifiers in columns.

Examining Data Frames

# Structure
str(experiment)
#> 'data.frame':    5 obs. of  4 variables:
#>  $ sample_id    : chr  "S1" "S2" "S3" "S4" ...
#>  $ treatment    : chr  "control" "drug_A" "drug_A" "drug_B" ...
#>  $ concentration: num  0 10 20 10 20
#>  $ response     : num  1.2 3.4 5.6 2.8 4.1

# Dimensions
dim(experiment)
#> [1] 5 4
nrow(experiment)
#> [1] 5
ncol(experiment)
#> [1] 4

# Preview
head(experiment, 3)
sample_id treatment concentration response
S1 control 0 1.2
S2 drug_A 10 3.4
S3 drug_A 20 5.6
tail(experiment, 2)
sample_id treatment concentration response
4 S4 drug_B 10 2.8
5 S5 drug_B 20 4.1

# Summary statistics
summary(experiment)
#>      sample_id     treatment concentration    response   
#>  Length   :5   Length   :5   Min.   : 0    Min.   :1.20  
#>  N.unique :5   N.unique :3   1st Qu.:10    1st Qu.:2.80  
#>  N.blank  :0   N.blank  :0   Median :10    Median :3.40  
#>  Min.nchar:2   Min.nchar:6   Mean   :12    Mean   :3.42  
#>  Max.nchar:2   Max.nchar:7   3rd Qu.:20    3rd Qu.:4.10  
#>                              Max.   :20    Max.   :5.60

# Column names
names(experiment)
#> [1] "sample_id"     "treatment"     "concentration" "response"
colnames(experiment)
#> [1] "sample_id"     "treatment"     "concentration" "response"

In Positron, you can also use View() to open a spreadsheet-like viewer (the Data Explorer) for interactive exploration:

# Opens data in the Data Explorer pane
View(experiment)
Tip

View() is useful for quick inspection, but avoid using it in scripts meant for non-interactive execution. For large datasets, View() can be slow—use head() instead.

Accessing Data Frame Elements

# Single column (returns vector)
experiment$response
#> [1] 1.2 3.4 5.6 2.8 4.1
experiment[["response"]]
#> [1] 1.2 3.4 5.6 2.8 4.1
experiment[, "response"]
#> [1] 1.2 3.4 5.6 2.8 4.1

# Multiple columns
experiment[, c("sample_id", "response")]
sample_id response
S1 1.2
S2 3.4
S3 5.6
S4 2.8
S5 4.1

# Rows by index
experiment[1, ]
sample_id treatment concentration response
S1 control 0 1.2
experiment[1:3, ]
sample_id treatment concentration response
S1 control 0 1.2
S2 drug_A 10 3.4
S3 drug_A 20 5.6

# Combination
experiment[1:3, c("treatment", "response")]
treatment response
control 1.2
drug_A 3.4
drug_A 5.6

# Conditional selection
experiment[experiment$treatment == "drug_A", ]
sample_id treatment concentration response
2 S2 drug_A 10 3.4
3 S3 drug_A 20 5.6
experiment[experiment$response > 3, ]
sample_id treatment concentration response
2 S2 drug_A 10 3.4
3 S3 drug_A 20 5.6
5 S5 drug_B 20 4.1

The [row, column] notation is the same one used for matrices: leaving a dimension empty selects everything along it, so experiment[, 2] is all rows of column 2 and experiment[2, ] is row 2 of every column. Conditional selection works because a comparison such as experiment$response > 3 returns a logical vector with one TRUE/FALSE per row, and a logical vector inside the brackets keeps the TRUE rows:

experiment$response > 3
#> [1] FALSE  TRUE  TRUE FALSE  TRUE

Of the three ways to pull out a column, experiment$response is the most readable and the one you will use most; experiment[["response"]] is the choice when the column name is stored in a variable; and experiment[, "response"] is the matrix-style form.

Adding and Modifying Columns

# Add a new column
experiment$replicate <- c(1, 1, 2, 1, 2)

# Calculated column
experiment$log_response <- log(experiment$response)

experiment
sample_id treatment concentration response replicate log_response
S1 control 0 1.2 1 0.1823216
S2 drug_A 10 3.4 1 1.2237754
S3 drug_A 20 5.6 2 1.7227666
S4 drug_B 10 2.8 1 1.0296194
S5 drug_B 20 4.1 2 1.4109870

11.11 Lists

Lists are flexible containers that can hold objects of different types and lengths. Unlike vectors, which require all elements to be the same type, lists can contain a mix of vectors, data frames, or even other lists.

Creating Lists

# Create individual vectors
measurements <- c(10, 20, 30, 40, 50)
categories <- c("high", "medium", "low")
status <- factor(c("active", "inactive"))

# Combine into a list
my_list <- list(
  values = measurements,
  labels = categories,
  status = status
)

my_list
#> $values
#> [1] 10 20 30 40 50
#> 
#> $labels
#> [1] "high"   "medium" "low"   
#> 
#> $status
#> [1] active   inactive
#> Levels: active inactive

# Check the structure
str(my_list)
#> List of 3
#>  $ values: num [1:5] 10 20 30 40 50
#>  $ labels: chr [1:3] "high" "medium" "low"
#>  $ status: Factor w/ 2 levels "active","inactive": 1 2

Indexing Lists

Lists use double square brackets [[]] to access individual elements:

# Access by position
my_list[[1]]
#> [1] 10 20 30 40 50

# Access by name
my_list[["labels"]]
#> [1] "high"   "medium" "low"

# Access using $ notation
my_list$values
#> [1] 10 20 30 40 50

# Access element within a list component
my_list[[1]][3]  # Third element of first component
#> [1] 30
Note

Single brackets [] return a sublist, while double brackets [[]] extract the actual element. This distinction matters when working with list components.

11.12 Matrices

Matrices are two-dimensional arrays where all elements must be the same type. Unlike data frames, matrices don’t have named columns by default and are optimized for mathematical operations.

Creating Matrices

# Create a matrix from a vector
mat <- matrix(1:12, nrow = 3, ncol = 4)
mat
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    4    7   10
#> [2,]    2    5    8   11
#> [3,]    3    6    9   12

# Create with row-wise filling
matrix(1:12, nrow = 3, ncol = 4, byrow = TRUE)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    2    3    4
#> [2,]    5    6    7    8
#> [3,]    9   10   11   12

# From vectors using cbind (column bind) or rbind (row bind)
col1 <- c(1, 2, 3)
col2 <- c(4, 5, 6)
cbind(col1, col2)
#>      col1 col2
#> [1,]    1    4
#> [2,]    2    5
#> [3,]    3    6

rbind(col1, col2)
#>      [,1] [,2] [,3]
#> col1    1    2    3
#> col2    4    5    6

Matrix Operations

# Dimensions
dim(mat)
#> [1] 3 4
nrow(mat)
#> [1] 3
ncol(mat)
#> [1] 4

# Transpose (swap rows and columns)
t(mat)
#>      [,1] [,2] [,3]
#> [1,]    1    2    3
#> [2,]    4    5    6
#> [3,]    7    8    9
#> [4,]   10   11   12

# Indexing: [row, column]
mat[1, 2]      # First row, second column
#> [1] 4
mat[1, ]       # Entire first row
#> [1]  1  4  7 10
mat[, 2]       # Entire second column
#> [1] 4 5 6
mat[1:2, 2:3]  # Submatrix
#>      [,1] [,2]
#> [1,]    4    7
#> [2,]    5    8

Matrices are particularly useful for linear algebra operations and when you need efficient numerical computations.

11.13 Reading and Writing Data

Reading Files

# CSV files (comma-separated)
measurements <- read.csv("data.csv")

# Tab-separated files
measurements <- read.table("data.tsv", header = TRUE, sep = "\t")

# Excel files (requires readxl package)
library(readxl)
measurements <- read_excel("data.xlsx")

# Specify options
measurements <- read.csv("data.csv",
                 header = TRUE,
                 stringsAsFactors = FALSE,
                 na.strings = c("", "NA", "N/A"))

read.csv() is a convenience wrapper around the general read.table(), and the arguments mean the same thing in both:

  • header = TRUE: the first line contains column names
  • sep: the delimiter between fields ("," for CSV, "\t" for tab-separated)
  • row.names = 1: treat the first column as row names rather than data
  • na.strings: which strings should become NA
  • stringsAsFactors: whether text columns become factors (the default has been FALSE since R 4.0)

The tidyverse’s readr package provides read_csv(), read_tsv(), and read_delim(), which are faster, never create row names or factors, report how each column was parsed, and return a tibble (an enhanced data frame). Both families are in everyday use and the readr versions are in Section 14.1.2; the base functions are handy because they need no packages at all, for instance in a script run on a cluster.

library(readr)
measurements <- read_csv("data.csv")    # note the underscore
measurements <- read_tsv("data.tsv")
WarningAlways check what you imported

Reading a file rarely fails loudly; it fails quietly by giving you the wrong column types. Immediately after any import, look at the result with str() or glimpse(), head(), dim(), and summary() (or View() in Positron). A numeric column that shows up as character means a non-numeric entry is hiding somewhere in the file.

Writing Files

# CSV output
write.csv(experiment, "experiment_results.csv", row.names = FALSE)

# Tab-separated
write.table(experiment, "results.tsv",
            sep = "\t",
            quote = FALSE,
            row.names = FALSE)

# readr equivalents (never write row names, never quote unnecessarily)
readr::write_csv(experiment, "experiment_results.csv")
readr::write_tsv(experiment, "results.tsv")

row.names = FALSE matters: without it, write.csv() adds an unnamed first column of row numbers that becomes a mystery column the next time the file is read. quote = FALSE keeps text fields free of quotation marks, which other programs (and shell tools such as cut and awk, Chapter 7) expect.

TipNever overwrite the raw data

Read the original data file, but write results to a new file with a different name. The raw file is the one thing in an analysis you cannot regenerate (Section 8.5.4).

11.14 Basic Visualization

R excels at creating graphics. Here’s a quick introduction to base R plotting; ggplot2, the approach used for the rest of the book, is covered in Chapter 13:

Scatter Plots

x <- 1:10
y <- x^2

plot(x, y, 
     main = "Quadratic Relationship",
     xlab = "X values",
     ylab = "Y values",
     col = "darkblue",
     pch = 16)  # Filled circles
Figure 11.6: Basic scatter plot showing quadratic relationship

Histograms

# Generate random data
values <- rnorm(1000, mean = 50, sd = 10)

hist(values,
     main = "Distribution of Values",
     xlab = "Value",
     col = "steelblue",
     breaks = 30)
Figure 11.7: Histogram of normally distributed data

Box Plots

# Create sample data
set.seed(42)
control <- rnorm(30, mean = 10, sd = 2)
treatment <- rnorm(30, mean = 15, sd = 3)

boxplot(control, treatment,
        names = c("Control", "Treatment"),
        main = "Treatment Effect",
        ylab = "Response",
        col = c("lightblue", "lightcoral"))
Figure 11.8: Box plot comparing treatment groups

When the data live in a data frame, the formula interface is more convenient: y ~ group reads as “y broken down by group,” and data = says where to find the columns. The same formula notation reappears in statistical models throughout R.

boxplot(response ~ treatment, data = experiment,
        xlab = "Treatment", ylab = "Response",
        col = "steelblue")
Figure 11.9: Box plot of response by treatment using the formula interface

Common Plot Arguments

Most base graphics functions share the arguments in Table 11.5.

Table 11.5: Common arguments to base R plotting functions
Argument Controls Example
main Plot title main = "Growth curve"
xlab, ylab Axis labels ylab = "OD600"
xlim, ylim Axis limits ylim = c(0, 1.5)
type "p" points, "l" lines, "b" both, "h" vertical bars type = "l"
col Color (name, hex code, or rgb()) col = "steelblue"
pch Point character, 0–25 (19 = filled circle) pch = 19
cex Size multiplier for points and text cex = 1.5
lwd, lty Line width and type lwd = 2, lty = "dashed"
breaks Number of bins (hist() only) breaks = 30
add = TRUE Draw onto the existing plot (curve(), lines(), points()) curve(dnorm(x), add = TRUE)

Graphical parameters such as col, pch, and cex are vectorized: give a vector with one entry per data point and each point gets its own value. Combined with ifelse() (Section 11.16.1) or a factor, this is how base graphics color points by group:

x <- seq(0, 10, by = 0.1)
y <- 10 - x
point_colors <- ifelse(x < 3, "firebrick", "steelblue")

plot(x, y, col = point_colors, pch = 19,
     xlab = "x", ylab = "10 - x", main = "Two-color scatterplot")
Figure 11.10: Vectorized graphical parameters: each point’s color and shape come from a vector of the same length as the data

With thousands of overlapping points, wrap any color in adjustcolor() to make it translucent — plot(x, y, pch = 16, col = adjustcolor("steelblue", alpha.f = 0.4)) — so that point density shows up as color density (the ggplot2 counterpart is alpha, Section 13.10.3).

Multiple Plots

par() sets graphical parameters that persist for the rest of the session; mfrow = c(rows, cols) divides the plotting window into a grid that successive plots fill left to right, top to bottom.

# Create 2x2 layout
par(mfrow = c(2, 2))

# Four different plots
plot(1:10, (1:10)^2, type = "l", main = "Line Plot")
hist(rnorm(100), main = "Histogram")
boxplot(rnorm(50), main = "Box Plot")
barplot(c(3, 5, 2, 7), names.arg = c("A", "B", "C", "D"), main = "Bar Plot")

# Reset to single plot
par(mfrow = c(1, 1))
Figure 11.11: Multiple plots in a single figure

Always reset with par(mfrow = c(1, 1)) (or save the old settings with old <- par(mfrow = c(2, 2)) and restore them with par(old)); otherwise every later plot in the session lands in the grid.

Tip

For publication-quality graphics, explore the ggplot2 package (Wickham, 2016), which provides a powerful and consistent grammar of graphics. We cover ggplot2 in detail in Chapter 13.

Saving Figures to Files

In Positron a plot lands in the Plots pane, but a figure that belongs in a paper, a report, or a results/ folder should be written to a file by the script, so that re-running the script regenerates it. Base R’s pattern is: open a file device, draw, then close the device with dev.off():

# Vector formats: sharp at any zoom - best for papers and posters
svg("results/boxplot.svg", width = 7, height = 5)        # inches
boxplot(response ~ treatment, data = my_data)
dev.off()                                                # ALWAYS close the device

pdf("results/boxplot.pdf", width = 7, height = 5)        # same pattern
boxplot(response ~ treatment, data = my_data)
dev.off()

# Raster format: a grid of pixels - needs a resolution; use for slides and the web
png("results/boxplot.png", width = 7, height = 5,
    units = "in", res = 300)
boxplot(response ~ treatment, data = my_data)
dev.off()

Everything drawn between the device call and dev.off() goes into the file instead of the Plots pane. The two classic mistakes are forgetting dev.off() — the file stays empty or locked, and later plots keep landing in it — and writing figures into data/ instead of results/ (raw data folders are read-only; see Section 11.13).

Choose the format by destination: vector formats (.svg, .pdf) store shapes and text and stay sharp at any size, which is what journals want; raster formats (.png at 300 dpi or more) store pixels and are right for slides and web pages. For ggplot2 figures, ggsave("results/fig.svg") wraps this whole pattern in one call (Chapter 13), and in Python matplotlib’s plt.savefig("fig.svg") picks the format from the file extension (Section 12.13).

11.15 The Split-Apply-Combine Approach

A common pattern in data analysis is to split data by groups, apply a function to each group, and combine the results. This split-apply-combine workflow appears repeatedly in scientific computing.

Using tapply() for Grouped Operations

The tapply() function applies a function to subsets of a vector, split by a factor:

# Sample data
values <- c(23, 45, 67, 34, 56, 78, 12, 89)
groups <- factor(c("A", "A", "A", "B", "B", "B", "C", "C"))

# Mean by group
tapply(values, groups, mean)
#>    A    B    C 
#> 45.0 56.0 50.5

# Standard deviation by group
tapply(values, groups, sd)
#>        A        B        C 
#> 22.00000 22.00000 54.44722

# Custom summary: range
tapply(values, groups, function(x) max(x) - min(x))
#>  A  B  C 
#> 44 44 77

Using aggregate() for Data Frames

For data frames, aggregate() summarizes multiple columns at once:

# Using built-in iris data
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa

# Mean of all numeric columns by Species
aggregate(. ~ Species, data = iris, FUN = mean)
Species Sepal.Length Sepal.Width Petal.Length Petal.Width
setosa 5.006 3.428 1.462 0.246
versicolor 5.936 2.770 4.260 1.326
virginica 6.588 2.974 5.552 2.026

# Multiple statistics (result contains a matrix column)
agg_result <- aggregate(Sepal.Length ~ Species, data = iris,
                        FUN = function(x) c(mean = mean(x), sd = sd(x)))

# Convert to a regular data frame for display
do.call(data.frame, agg_result)
Species Sepal.Length.mean Sepal.Length.sd
setosa 5.006 0.3524897
versicolor 5.936 0.5161711
virginica 6.588 0.6358796

The apply() Family

R provides several related functions for different data structures (Table 11.6):

Table 11.6: The apply family of functions
Function Input Applies Over
apply() Matrix/data frame Rows or columns
lapply() List Each element, returns list
sapply() List Each element, returns vector
tapply() Vector + factor Groups defined by factor
mapply() Multiple vectors Corresponding elements
# Apply to matrix columns
mat <- matrix(1:12, nrow = 3)
apply(mat, 2, sum)  # Column sums (MARGIN = 2)
#> [1]  6 15 24 33
apply(mat, 1, sum)  # Row sums (MARGIN = 1)
#> [1] 22 26 30

# Apply to each element of a list
my_list <- list(a = 1:5, b = 10:15, c = 100:110)
lapply(my_list, mean)
#> $a
#> [1] 3
#> 
#> $b
#> [1] 12.5
#> 
#> $c
#> [1] 105
sapply(my_list, mean)  # Simplified output
#>     a     b     c 
#>   3.0  12.5 105.0
Tip

The tidyverse packages (covered in Chapter 14) provide more intuitive alternatives like group_by() and summarize() for this workflow.

11.16 Basic Programming Constructs

As you develop your R skills, you’ll encounter situations requiring conditional logic and iteration.

Conditional Statements with ifelse()

The ifelse() function provides vectorized conditional logic:

# Basic ifelse
scores <- c(85, 72, 91, 68, 79)
ifelse(scores >= 80, "Pass", "Fail")
#> [1] "Pass" "Fail" "Pass" "Fail" "Fail"

# Nested conditions
grades <- ifelse(scores >= 90, "A",
           ifelse(scores >= 80, "B",
             ifelse(scores >= 70, "C", "F")))
grades
#> [1] "B" "C" "A" "F" "C"

# Useful for creating indicator variables
treatment <- c("control", "drug", "drug", "control", "drug")
colors <- ifelse(treatment == "drug", "red", "blue")

Type-Safe Conditionals with if_else()

The tidyverse provides dplyr::if_else(), which is stricter than base R’s ifelse():

library(dplyr)

x <- c(-2, -1, 0, 1, 2)

# if_else() requires matching types for true/false
if_else(x > 0, "positive", "non-positive")
#> [1] "non-positive" "non-positive" "non-positive" "positive"     "positive"

# Handles NA explicitly with the 'missing' argument
y <- c(1, NA, 3, NA, 5)
if_else(y > 2, "high", "low", missing = "unknown")
#> [1] "low"     "unknown" "high"    "unknown" "high"

# Creates a simple absolute value implementation
if_else(x < 0, -x, x)
#> [1] 2 1 0 1 2
Tip

Use dplyr::if_else() over base ifelse() when:

  • You want type checking (prevents accidentally mixing numbers and strings)
  • You need explicit control over NA handling
  • You’re already using tidyverse functions

For Loops

While R is optimized for vectorized operations, loops are sometimes necessary:

# Simple loop
for (i in 1:5) {
  print(i^2)
}
#> [1] 1
#> [1] 4
#> [1] 9
#> [1] 16
#> [1] 25

# Loop with pre-allocated output (recommended for efficiency)
n <- 10
results <- numeric(n)  # Pre-allocate
for (i in 1:n) {
  results[i] <- i * 2
}
results
#>  [1]  2  4  6  8 10 12 14 16 18 20
Warning

In R, loops are often slower than vectorized alternatives. Before writing a loop, consider whether a vectorized function (apply(), tapply(), etc.) or built-in operation would work instead.

Writing Simple Functions

You can create your own functions for repeated tasks (Chapter 15 treats this in depth):

# Define a function
calculate_cv <- function(x) {
  # Coefficient of variation: SD / mean * 100
  cv <- sd(x) / mean(x) * 100
  return(cv)
}

# Use the function
readings <- c(10, 12, 11, 13, 9, 14)
calculate_cv(readings)
#> [1] 16.26808

# Function with multiple arguments and default values
summarize_data <- function(x, digits = 2) {
  result <- c(
    mean = round(mean(x), digits),
    sd = round(sd(x), digits),
    n = length(x)
  )
  return(result)
}

summarize_data(readings)
#>  mean    sd     n 
#> 11.50  1.87  6.00
summarize_data(readings, digits = 3)
#>   mean     sd      n 
#> 11.500  1.871  6.000

11.17 Random Sampling and Simulation

R makes it easy to generate random data and run simulations:

# Set seed for reproducibility
set.seed(123)

# Random samples from distributions
uniform_sample <- runif(100, min = 0, max = 1)
normal_sample <- rnorm(1000, mean = 0, sd = 1)
poisson_sample <- rpois(100, lambda = 5)
binomial_sample <- rbinom(100, size = 20, prob = 0.3)

# Visualize normal distribution
hist(normal_sample, 
     probability = TRUE,  # density scale, not counts (freq = FALSE is equivalent)
     main = "Sample vs. Theoretical Distribution",
     col = "lightblue")

# Add theoretical curve
curve(dnorm(x, mean = 0, sd = 1), 
      add = TRUE, 
      col = "red", 
      lwd = 2)
Figure 11.12: Simulated data from a normal distribution with theoretical curve overlay

probability = TRUE (equivalently freq = FALSE) puts the histogram on the density scale, which is why the theoretical dnorm() curve overlays directly with no rescaling. The binomial draw above shows the fourth workhorse distribution: size tries, each succeeding with probability prob.

Common Distribution Functions

For distribution xxx (e.g., norm, unif, pois, binom):

  • rxxx() — Random samples
  • dxxx() — Density/probability
  • pxxx() — Cumulative distribution function
  • qxxx() — Quantile function
# Normal distribution
rnorm(5)        # 5 random values
#> [1] -0.05697053 -0.95784939  0.59106191  0.17310487  1.39978336
dnorm(0)        # Density at x=0
#> [1] 0.3989423
pnorm(1.96)     # P(X ≤ 1.96)
#> [1] 0.9750021
qnorm(0.975)    # Value where P(X ≤ x) = 0.975
#> [1] 1.959964

Repeated Simulations with replicate()

The replicate() function repeats an expression multiple times and collects the results—perfect for simulations (Figure 11.13):

# Shuffle integers 1-10 five times
set.seed(42)
replicate(5, sample(1:10, size = 10, replace = FALSE))
#>       [,1] [,2] [,3] [,4] [,5]
#>  [1,]    1    8    9    3    5
#>  [2,]    5    7   10    1    4
#>  [3,]   10    4    3    2    2
#>  [4,]    8    1    4    6    8
#>  [5,]    2    5    5   10    3
#>  [6,]    4   10    6    8    1
#>  [7,]    6    2    1    4   10
#>  [8,]    9    6    2    5    7
#>  [9,]    7    9    8    7    6
#> [10,]    3    3    7    9    9

# Simulate sampling distributions
# Take 1000 samples of size 30 and calculate mean of each
set.seed(123)
sample_means <- replicate(1000, mean(rnorm(30, mean = 100, sd = 15)))
hist(sample_means, main = "Distribution of Sample Means",
     col = "lightgreen", xlab = "Sample Mean")
Figure 11.13: Sampling distribution of the mean from 1000 samples of size 30 generated with replicate()

The replicate() function belongs to the “apply” family and returns a matrix (if results are vectors of equal length) or a list (if results vary in length).

11.18 Useful Numeric Transformations

R and the tidyverse provide many functions for transforming numeric data. These are particularly useful within mutate() operations.

Cumulative and Running Aggregates

Base R provides functions for running (cumulative) calculations:

x <- c(1, 2, 3, 4, 5)

cumsum(x)   # Running sum: 1, 3, 6, 10, 15
#> [1]  1  3  6 10 15
cumprod(x)  # Running product: 1, 2, 6, 24, 120
#> [1]   1   2   6  24 120
cummax(x)   # Running maximum
#> [1] 1 2 3 4 5
cummin(x)   # Running minimum
#> [1] 1 1 1 1 1

# Useful for time series data
daily_cases <- c(10, 15, 8, 22, 18)
cumsum(daily_cases)  # Total cases over time
#> [1] 10 25 33 55 73

Offsets with lead() and lag()

The dplyr::lead() and dplyr::lag() functions let you reference values before or after the current position:

library(dplyr)

x <- c(10, 20, 30, 40, 50)

lag(x)   # Previous value: NA, 10, 20, 30, 40
#> [1] NA 10 20 30 40
lead(x)  # Next value: 20, 30, 40, 50, NA
#> [1] 20 30 40 50 NA

# Calculate differences from previous value
x - lag(x)
#> [1] NA 10 10 10 10

# Detect changes
x != lag(x)
#> [1]   NA TRUE TRUE TRUE TRUE

# Lag by more than one position
lag(x, n = 2)
#> [1] NA NA 10 20 30

This is invaluable for time series analysis, detecting changes, and calculating growth rates.

Ranking Values

dplyr provides several ranking functions:

x <- c(5, 1, 3, 2, 2, NA)

min_rank(x)      # Standard competition ranking (1, 2, 2, 4)
#> [1]  5  1  4  2  2 NA
dense_rank(x)    # No gaps after ties (1, 2, 2, 3)
#> [1]  4  1  3  2  2 NA
row_number(x)    # Unique ranks (ties broken by position)
#> [1]  5  1  4  2  3 NA

# Rank in descending order
min_rank(desc(x))
#> [1]  1  5  2  3  3 NA

# Practical example: find top 3 values
expr <- tibble(gene = c("A", "B", "C", "D", "E"),
               expression = c(5.2, 8.1, 3.4, 9.7, 6.5))
expr |> filter(min_rank(desc(expression)) <= 3)
gene expression
B 8.1
D 9.7
E 6.5

Binning Numbers with cut()

The cut() function divides continuous data into discrete bins:

ages <- c(15, 25, 35, 45, 55, 65, 75)

# Create age groups
cut(ages, breaks = c(0, 18, 35, 55, 100))
#> [1] (0,18]   (18,35]  (18,35]  (35,55]  (35,55]  (55,100] (55,100]
#> Levels: (0,18] (18,35] (35,55] (55,100]

# Custom labels
cut(ages,
    breaks = c(0, 18, 35, 55, 100),
    labels = c("minor", "young_adult", "middle_aged", "senior"))
#> [1] minor       young_adult young_adult middle_aged middle_aged senior     
#> [7] senior     
#> Levels: minor young_adult middle_aged senior

# Include lowest value and control interval direction
cut(ages, breaks = c(0, 18, 35, 55, 100),
    include.lowest = TRUE, right = FALSE)
#> [1] [0,18)   [18,35)  [35,55)  [35,55)  [55,100] [55,100] [55,100]
#> Levels: [0,18) [18,35) [35,55) [55,100]

This is particularly useful for creating categorical variables from continuous measurements for analysis or visualization.

11.19 Cleaning Up Your Environment

As you work in R, your environment accumulates objects. Periodically cleaning up helps manage memory and avoid confusion.

Removing Objects

Use rm() to remove objects from your environment:

# Remove a single object
rm(x)

# Remove multiple objects
rm(x, y, z)

# Remove all objects (use with caution!)
rm(list = ls())
Warning

rm(list = ls()) removes everything in your environment. Use it intentionally, typically at the start of a script to ensure a clean slate, but never in a shared function or package.

Closing Graphics Devices

When working with plots, graphics devices can accumulate. Use dev.off() to close them:

# Close the current graphics device
dev.off()

# Close all graphics devices
graphics.off()

# List open devices
dev.list()

This is especially important when saving plots to files—if you don’t close the device, the file may not be properly saved:

# Save a plot to PDF
pdf("my_plot.pdf")
plot(1:10, (1:10)^2)
dev.off()  # Essential! Closes the file and finalizes it

# Similarly for PNG
png("my_plot.png", width = 800, height = 600)
plot(1:10, (1:10)^2)
dev.off()

Checking Your Environment

# List all objects in the environment
ls()

# List objects matching a pattern
ls(pattern = "data")

# Check if an object exists
exists("my_variable")

# See environment size
object.size(x)  # Size of one object

11.20 Running Example: The Stickleback Data

NoteRunning example

This section applies the chapter to the course dataset described in Section 1.5 (80 stickleback, 4 treatment groups, 600 genes). The same steps appear on the Week 6 slides, so the book and the lecture can be read side by side.

The whole table fits comfortably in an R data frame. read.delim() reads tab-separated files with a header; check.names = FALSE keeps the column name Geno&Micro exactly as it is in the file (otherwise R would rename it Geno.Micro), and backticks let you refer to such a name later.

stickle <- read.delim("../data/Stickle_RNAseq.tsv", check.names = FALSE)
dim(stickle)                          # 80 fish x 604 columns
#> [1]  80 604
stickle[1:4, 1:7]                     # the first rows and columns
Individual Genotype Microbiota Geno&Micro Gene1 Gene2 Gene3
AC1 A conventional convA 16 15 71
AC2 A conventional convA 7 6 72
AC3 A conventional convA 9 8 22
AC4 A conventional convA 8 18 15
table(stickle$Genotype, stickle$Microbiota)
#>    
#>     conventional mono
#>   A           20   20
#>   B           20   20
summary(stickle$Gene1)
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>   0.000   1.000   3.500   7.675  12.250  39.000

A first figure takes one line of base R. The formula Gene1 ~ Genotype + Microbiota asks for one box per combination of the two factors:

boxplot(Gene1 ~ Genotype + Microbiota, data = stickle,
        col = c("steelblue", "tomato", "steelblue", "tomato"),
        ylab = "Gene1 read count", las = 2)
Figure 11.14: Gene1 read counts by host genotype and microbiota treatment, drawn with base R’s boxplot().

Genotype A fish have far higher Gene1 counts than genotype B, and within each genotype the mono-associated fish sit a little higher, the same pattern the awk script in Chapter 10 found. Chapter 14 redoes this with the tidyverse for all 600 genes at once, and Chapter 13 turns the figure into publication-quality ggplot2.

11.21 Summary

This chapter introduced R programming fundamentals:

  • R is designed for statistical computing with excellent graphics
  • Everything in R is an object with a class and type; use class(), typeof(), and str() to explore objects
  • Positron provides an integrated development environment with a console, Variables pane, plots pane, and data viewer
  • Variables are assigned with <-; R is case-sensitive; avoid reserved words and shadowing built-in functions
  • Vectors are the fundamental data structure; operations are vectorized
  • Logical operators (&, |, !, %in%) and comparison operators enable conditional logic
  • Floating-point comparisons require all.equal() instead of ==
  • NA propagates through calculations; test for it with is.na(), never == NA, and drop it with na.rm = TRUE
  • Data frames hold tabular data with columns of different types
  • Lists can contain elements of different types and lengths
  • Matrices are optimized for numerical computations
  • Functions perform operations; use ?function, vignettes, and demos for help, read error messages from the bottom up, and ask with a minimal reproducible example
  • Packages extend R’s capabilities; watch for namespace conflicts when loading multiple packages
  • The split-apply-combine pattern (tapply(), aggregate(), apply()) is fundamental to data analysis
  • Conditional logic (ifelse()) and loops enable programmatic control
  • Writing custom functions allows you to encapsulate repeated operations
  • R reads/writes CSV, TSV, and Excel files easily
  • Basic plotting is built-in; ggplot2 offers advanced graphics
  • Use rm() and dev.off() to clean up your environment and graphics devices

These fundamentals prepare you for more advanced R programming, including the tidyverse packages covered in Chapter 14 and Chapter 13. A command reference is in Appendix E.

Tip

For a comprehensive reference of R commands and functions, including base R and tidyverse, see Appendix E.

Exercises

TipPractice Exercises

Exercise 1: Vector Operations

  1. Create a vector of the first 20 integers
  2. Calculate the mean, median, and standard deviation
  3. Create a new vector containing only the even numbers
  4. Calculate the sum of squares

Exercise 2: Data Frame Practice

Create a data frame representing an experiment: 1. Include columns for: sample_id, group (control/treatment), measurement 2. Add 10 rows of sample data 3. Calculate the mean measurement for each group 4. Add a new column with log-transformed measurements

Exercise 3: Lists and Matrices

  1. Create a list containing three components: a numeric vector, a character vector, and a data frame
  2. Use indexing to extract the second element of the first component
  3. Create a 4x4 matrix filled with the numbers 1-16
  4. Calculate row sums and column means using apply()

Exercise 4: Split-Apply-Combine

Using the built-in mtcars dataset: 1. Calculate the mean mpg for each number of cylinders (cyl) using tapply() 2. Use aggregate() to find the mean and standard deviation of hp by cyl 3. Write a custom function that returns the range (max - min) of a vector 4. Apply your function to find the range of mpg for each gear type

Exercise 5: Programming Practice

  1. Use ifelse() to create a new vector that categorizes mtcars$mpg as “efficient” (≥25) or “inefficient” (<25)
  2. Write a for loop that calculates the cumulative sum of the first 10 integers
  3. Create a function called standardize() that takes a vector and returns z-scores: (x - mean) / sd
  4. Test your function on a vector of your choice

Exercise 6: Random Simulation

  1. Generate 1000 random samples from a normal distribution with mean=100 and sd=15
  2. Create a histogram of the data
  3. Calculate what proportion of values fall between 85 and 115
  4. Compare to the theoretical proportion for a normal distribution

Exercise 7: File I/O

  1. Create a data frame with experimental data
  2. Save it as a CSV file
  3. Read it back into a new variable
  4. Verify the data matches the original
  5. Repeat the round trip with write.csv() without row.names = FALSE, read the file back, and explain the extra column

Exercise 8: Exploring Your IDE and the Help System

  1. Locate the editor, Console, Variables, and Plots panes; open the Help pane
  2. Create a new R script and a new Quarto document (File → New File) and compare them
  3. Run ?sd, then args(sd) and example(sd); from the help page alone, work out how to compute the standard deviation of a vector containing NA
  4. Deliberately trigger three errors from Table 11.4 (an undefined object, a missing package, an unbalanced bracket) and confirm you can read each message

Exercise 9: Base Plotting

  1. Create a sequence with seq() and plot it three times, changing type to "p", "l", and "b"
  2. Generate 1000 values with rnorm() and draw histograms with breaks = 10 and breaks = 50; what does breaks control?
  3. Use par(mfrow = c(2, 2)) to place all four plots in one figure, then reset the layout
  4. Color the points in a scatterplot by whether they exceed a threshold, using a vector for col

Additional Resources

Official Documentation and Tutorials

Quick References

Bioinformatics-Specific

Recommended Books

  • Logan, M. 2010. Biostatistical Design and Analysis Using R — Excellent introduction to R for statistical analysis in the life sciences
  • Grolemund, G. 2014. Hands-On Programming with R (Grolemund, 2014) — A gentle, project-based introduction to programming concepts in R