Go back to the main page
Go back to the R overview page


This Rmd file can be downloaded here

Unlike the tidyverse, base R needs no extra packages — everything below uses functions that ship with R by default.

📘 Base R Overview

💡 This page provides a structured overview of core base R functions, covering the same operations as the Tidyverse Overview page, but without relying on any external packages. While this overview includes references and examples using base R for completeness and historical context, for modern data science workflows, the use of the Tidyverse suite of packages (such as dplyr, tidyr, and ggplot2) is strongly recommended and advised. While base R is powerful, its syntax can occasionally be inconsistent—such as automatically dropping dimensions when subsetting single columns. The Tidyverse avoids these pitfalls by maintaining predictable data structures (like tibbles) and utilizing a clean, pipe-oriented (%>% or |>) syntax. This approach significantly enhances code readability, makes your scripts easier to maintain, and ensures a more consistent programming experience. Click a category to expand and jump to detailed examples.

The examples shown are performed on the Puromycin dataset. The Puromycin dataset is a classic example dataset built into the base R installation (within the datasets package). It originates from a biochemical experiment and is widely used for demonstrating and testing nonlinear regression models, particularly the Michaelis-Menten kinetics model in enzyme reactions.

head(Puromycin)
##   conc rate   state
## 1 0.02   76 treated
## 2 0.02   47 treated
## 3 0.06   97 treated
## 4 0.06  107 treated
## 5 0.11  123 treated
## 6 0.11  139 treated

Summary Table

💡 This section summarizes the core base R operations on data frames. Click on the triangles to expand.

🧩 Row Operations — na.omit(), subset()/[ ], order(), unique(), sample(), which.max()/which.min() Select, subset, or reorder rows.
➡️ See Row Actions ↓
📊 Column Operations — [ , ], names(), transform(), column reordering, sapply()/lapply() Choose, rename, or transform columns.
➡️ See Column Actions ↓
🧮 Grouping & Aggregation — split(), aggregate(), ave(), tapply() Split data into groups and compute summaries.
➡️ See Group Actions ↓
🔗 Joins & Binding — merge(), rbind(), cbind() Combine multiple data frames.
➡️ See Join Actions ↓
🔄 Reshaping Data — reshape() Restructure data between long and wide formats.
➡️ See Reshaping ↓
📈 Visualization — plot(), points(), lines(), legend() Create and customize plots.
➡️ See Visualization ↓
🧰 Utilities — str(), $/[[ ]], table() Inspect or extract data quickly.
➡️ See Utilities ↓

Function-Specific Actions with Examples


🧩 Actions on Rows

⬆️ Back to Summary

💡 This section lists core row operations in base R. Click on the triangles to expand.

na.omit() – Drop rows containing NA values

First create a copy with an NA value:

Puro_with_NA <- Puromycin # create a copy
Puro_with_NA[1, 2] <- NA # replace row 1, column 2 with NA
Puro_with_NA
##    conc rate     state
## 1  0.02   NA   treated
## 2  0.02   47   treated
## 3  0.06   97   treated
## 4  0.06  107   treated
## 5  0.11  123   treated
## 6  0.11  139   treated
## 7  0.22  159   treated
## 8  0.22  152   treated
## 9  0.56  191   treated
## 10 0.56  201   treated
## 11 1.10  207   treated
## 12 1.10  200   treated
## 13 0.02   67 untreated
## 14 0.02   51 untreated
## 15 0.06   84 untreated
## 16 0.06   86 untreated
## 17 0.11   98 untreated
## 18 0.11  115 untreated
## 19 0.22  131 untreated
## 20 0.22  124 untreated
## 21 0.56  144 untreated
## 22 0.56  158 untreated
## 23 1.10  160 untreated

And drop the row with the NA:

# Drop row with NA
na.omit(Puro_with_NA)
##    conc rate     state
## 2  0.02   47   treated
## 3  0.06   97   treated
## 4  0.06  107   treated
## 5  0.11  123   treated
## 6  0.11  139   treated
## 7  0.22  159   treated
## 8  0.22  152   treated
## 9  0.56  191   treated
## 10 0.56  201   treated
## 11 1.10  207   treated
## 12 1.10  200   treated
## 13 0.02   67 untreated
## 14 0.02   51 untreated
## 15 0.06   84 untreated
## 16 0.06   86 untreated
## 17 0.11   98 untreated
## 18 0.11  115 untreated
## 19 0.22  131 untreated
## 20 0.22  124 untreated
## 21 0.56  144 untreated
## 22 0.56  158 untreated
## 23 1.10  160 untreated
subset() / [ ] – Subset rows based on conditions
# Keep only rows where rate > 200
subset(Puromycin, rate > 200)
##    conc rate   state
## 10 0.56  201 treated
## 11 1.10  207 treated
Selects rows that meet logical conditions. The equivalent indexing form is Puromycin[Puromycin$rate > 200, ]. Supports multiple conditions with & (and) or | (or).
[ ] – Select rows by position
# Keep the first 5 rows
Puromycin[1:5, ]
##   conc rate   state
## 1 0.02   76 treated
## 2 0.02   47 treated
## 3 0.06   97 treated
## 4 0.06  107 treated
## 5 0.11  123 treated
Select rows by numeric positions, e.g. with head(), tail(), or a vector of row indices.
order() – Reorder rows by variable(s)
# Sort by rate descending
Puromycin[order(-Puromycin$rate), ]
##    conc rate     state
## 11 1.10  207   treated
## 10 0.56  201   treated
## 12 1.10  200   treated
## 9  0.56  191   treated
## 23 1.10  160 untreated
## 7  0.22  159   treated
## 22 0.56  158 untreated
## 8  0.22  152   treated
## 21 0.56  144 untreated
## 6  0.11  139   treated
## 19 0.22  131 untreated
## 20 0.22  124 untreated
## 5  0.11  123   treated
## 18 0.11  115 untreated
## 4  0.06  107   treated
## 17 0.11   98 untreated
## 3  0.06   97   treated
## 16 0.06   86 untreated
## 15 0.06   84 untreated
## 1  0.02   76   treated
## 13 0.02   67 untreated
## 14 0.02   51 untreated
## 2  0.02   47   treated
Orders rows by one or more columns. Use a minus sign (or decreasing = TRUE) for descending order.
unique() – Keep unique rows
# Keep unique values of rate
unique(Puromycin["rate"])
##    rate
## 1    76
## 2    47
## 3    97
## 4   107
## 5   123
## 6   139
## 7   159
## 8   152
## 9   191
## 10  201
## 11  207
## 12  200
## 13   67
## 14   51
## 15   84
## 16   86
## 17   98
## 18  115
## 19  131
## 20  124
## 21  144
## 22  158
## 23  160
Removes duplicate rows based on one or more columns.
sample() – Random sample of rows
# Randomly sample 3 rows
Puromycin[sample(nrow(Puromycin), 3), ]
##    conc rate     state
## 20 0.22  124 untreated
## 13 0.02   67 untreated
## 4  0.06  107   treated
Randomly selects rows; sample a fraction by basing size on round(nrow(Puromycin) * prop).
order() + head() – Top or bottom rows by value
# 3 rows with highest rate
head(Puromycin[order(-Puromycin$rate), ], 3)
##    conc rate   state
## 11 1.10  207 treated
## 10 0.56  201 treated
## 12 1.10  200 treated
Select rows with minimum or maximum values of a variable by ordering first, then taking the head or tail.

📊 Actions on Columns

⬆️ Back to Summary

💡 This section lists core column operations in base R. Click on the triangles to expand.

[ , c() ] – Choose specific columns
# Select columns conc and rate
Puromycin[, c("conc", "rate")]
##    conc rate
## 1  0.02   76
## 2  0.02   47
## 3  0.06   97
## 4  0.06  107
## 5  0.11  123
## 6  0.11  139
## 7  0.22  159
## 8  0.22  152
## 9  0.56  191
## 10 0.56  201
## 11 1.10  207
## 12 1.10  200
## 13 0.02   67
## 14 0.02   51
## 15 0.06   84
## 16 0.06   86
## 17 0.11   98
## 18 0.11  115
## 19 0.22  131
## 20 0.22  124
## 21 0.56  144
## 22 0.56  158
## 23 1.10  160
Keeps only the specified columns. Helper functions like grepl() on names(Puromycin) can emulate starts_with(), ends_with(), or contains().
names() – Rename columns
# Rename conc to conc_ppm
Puromycin_renamed <- Puromycin
names(Puromycin_renamed)[names(Puromycin_renamed) == "conc"] <- "conc_ppm"
Puromycin_renamed
##    conc_ppm rate     state
## 1      0.02   76   treated
## 2      0.02   47   treated
## 3      0.06   97   treated
## 4      0.06  107   treated
## 5      0.11  123   treated
## 6      0.11  139   treated
## 7      0.22  159   treated
## 8      0.22  152   treated
## 9      0.56  191   treated
## 10     0.56  201   treated
## 11     1.10  207   treated
## 12     1.10  200   treated
## 13     0.02   67 untreated
## 14     0.02   51 untreated
## 15     0.06   84 untreated
## 16     0.06   86 untreated
## 17     0.11   98 untreated
## 18     0.11  115 untreated
## 19     0.22  131 untreated
## 20     0.22  124 untreated
## 21     0.56  144 untreated
## 22     0.56  158 untreated
## 23     1.10  160 untreated
Changes column names without altering the data. A copy is made first so the original Puromycin data frame stays untouched.
transform() – Add or modify columns
# Add a new column rate_per_conc
transform(Puromycin, rate_per_conc = rate / conc)
##    conc rate     state rate_per_conc
## 1  0.02   76   treated     3800.0000
## 2  0.02   47   treated     2350.0000
## 3  0.06   97   treated     1616.6667
## 4  0.06  107   treated     1783.3333
## 5  0.11  123   treated     1118.1818
## 6  0.11  139   treated     1263.6364
## 7  0.22  159   treated      722.7273
## 8  0.22  152   treated      690.9091
## 9  0.56  191   treated      341.0714
## 10 0.56  201   treated      358.9286
## 11 1.10  207   treated      188.1818
## 12 1.10  200   treated      181.8182
## 13 0.02   67 untreated     3350.0000
## 14 0.02   51 untreated     2550.0000
## 15 0.06   84 untreated     1400.0000
## 16 0.06   86 untreated     1433.3333
## 17 0.11   98 untreated      890.9091
## 18 0.11  115 untreated     1045.4545
## 19 0.22  131 untreated      595.4545
## 20 0.22  124 untreated      563.6364
## 21 0.56  144 untreated      257.1429
## 22 0.56  158 untreated      282.1429
## 23 1.10  160 untreated      145.4545
Creates new columns or modifies existing ones using expressions, returning a new data frame.
data.frame() – Create new columns and drop others
# Keep only rate_per_conc column
data.frame(rate_per_conc = Puromycin$rate / Puromycin$conc)
##    rate_per_conc
## 1      3800.0000
## 2      2350.0000
## 3      1616.6667
## 4      1783.3333
## 5      1118.1818
## 6      1263.6364
## 7       722.7273
## 8       690.9091
## 9       341.0714
## 10      358.9286
## 11      188.1818
## 12      181.8182
## 13     3350.0000
## 14     2550.0000
## 15     1400.0000
## 16     1433.3333
## 17      890.9091
## 18     1045.4545
## 19      595.4545
## 20      563.6364
## 21      257.1429
## 22      282.1429
## 23      145.4545
Generates a new data frame containing only the computed column(s), dropping everything else.
column reordering – Move columns to new positions
# Move state column to the first position
Puromycin[, c("state", setdiff(names(Puromycin), "state"))]
##        state conc rate
## 1    treated 0.02   76
## 2    treated 0.02   47
## 3    treated 0.06   97
## 4    treated 0.06  107
## 5    treated 0.11  123
## 6    treated 0.11  139
## 7    treated 0.22  159
## 8    treated 0.22  152
## 9    treated 0.56  191
## 10   treated 0.56  201
## 11   treated 1.10  207
## 12   treated 1.10  200
## 13 untreated 0.02   67
## 14 untreated 0.02   51
## 15 untreated 0.06   84
## 16 untreated 0.06   86
## 17 untreated 0.11   98
## 18 untreated 0.11  115
## 19 untreated 0.22  131
## 20 untreated 0.22  124
## 21 untreated 0.56  144
## 22 untreated 0.56  158
## 23 untreated 1.10  160
Reorders columns by re-indexing with a reordered vector of column names, without changing the data.
sapply() / lapply() – Apply functions to multiple columns
# Convert all numeric columns to log scale
Puromycin_log <- Puromycin
numeric_cols <- sapply(Puromycin_log, is.numeric)
Puromycin_log[numeric_cols] <- lapply(Puromycin_log[numeric_cols], log)
Puromycin_log
##           conc     rate     state
## 1  -3.91202301 4.330733   treated
## 2  -3.91202301 3.850148   treated
## 3  -2.81341072 4.574711   treated
## 4  -2.81341072 4.672829   treated
## 5  -2.20727491 4.812184   treated
## 6  -2.20727491 4.934474   treated
## 7  -1.51412773 5.068904   treated
## 8  -1.51412773 5.023881   treated
## 9  -0.57981850 5.252273   treated
## 10 -0.57981850 5.303305   treated
## 11  0.09531018 5.332719   treated
## 12  0.09531018 5.298317   treated
## 13 -3.91202301 4.204693 untreated
## 14 -3.91202301 3.931826 untreated
## 15 -2.81341072 4.430817 untreated
## 16 -2.81341072 4.454347 untreated
## 17 -2.20727491 4.584967 untreated
## 18 -2.20727491 4.744932 untreated
## 19 -1.51412773 4.875197 untreated
## 20 -1.51412773 4.820282 untreated
## 21 -0.57981850 4.969813 untreated
## 22 -0.57981850 5.062595 untreated
## 23  0.09531018 5.075174 untreated
Applies a function across selected columns; sapply() identifies which columns qualify, and lapply() applies the transformation.

🧮 Actions on Groups

⬆️ Back to Summary

💡 This section lists core grouping operations in base R. Click on the triangles to expand.

split() – Group data by one or more variables
# Group by conc
split(Puromycin, Puromycin$conc)
## $`0.02`
##    conc rate     state
## 1  0.02   76   treated
## 2  0.02   47   treated
## 13 0.02   67 untreated
## 14 0.02   51 untreated
## 
## $`0.06`
##    conc rate     state
## 3  0.06   97   treated
## 4  0.06  107   treated
## 15 0.06   84 untreated
## 16 0.06   86 untreated
## 
## $`0.11`
##    conc rate     state
## 5  0.11  123   treated
## 6  0.11  139   treated
## 17 0.11   98 untreated
## 18 0.11  115 untreated
## 
## $`0.22`
##    conc rate     state
## 7  0.22  159   treated
## 8  0.22  152   treated
## 19 0.22  131 untreated
## 20 0.22  124 untreated
## 
## $`0.56`
##    conc rate     state
## 9  0.56  191   treated
## 10 0.56  201   treated
## 21 0.56  144 untreated
## 22 0.56  158 untreated
## 
## $`1.1`
##    conc rate     state
## 11  1.1  207   treated
## 12  1.1  200   treated
## 23  1.1  160 untreated
Splits the data frame into a list of data frames, one per group, for subsequent manipulation.
do.call(rbind, …) – Remove grouping
# Recombine the grouped list back into a single data frame
do.call(rbind, split(Puromycin, Puromycin$conc))
##         conc rate     state
## 0.02.1  0.02   76   treated
## 0.02.2  0.02   47   treated
## 0.02.13 0.02   67 untreated
## 0.02.14 0.02   51 untreated
## 0.06.3  0.06   97   treated
## 0.06.4  0.06  107   treated
## 0.06.15 0.06   84 untreated
## 0.06.16 0.06   86 untreated
## 0.11.5  0.11  123   treated
## 0.11.6  0.11  139   treated
## 0.11.17 0.11   98 untreated
## 0.11.18 0.11  115 untreated
## 0.22.7  0.22  159   treated
## 0.22.8  0.22  152   treated
## 0.22.19 0.22  131 untreated
## 0.22.20 0.22  124 untreated
## 0.56.9  0.56  191   treated
## 0.56.10 0.56  201   treated
## 0.56.21 0.56  144 untreated
## 0.56.22 0.56  158 untreated
## 1.1.11  1.10  207   treated
## 1.1.12  1.10  200   treated
## 1.1.23  1.10  160 untreated
Base R has no persistent “grouped” object the way tibbles do — a list created by split() is just recombined with rbind() when grouping is no longer needed.
aggregate() – Compute summary statistics by group
# Average rate by conc
aggregate(rate ~ conc, data = Puromycin, FUN = mean)
##   conc   rate
## 1 0.02  60.25
## 2 0.06  93.50
## 3 0.11 118.75
## 4 0.22 141.50
## 5 0.56 173.50
## 6 1.10 189.00
Aggregates data within each group to produce summary statistics, similar to group_by() %>% summarize().
ave() – Add or modify columns within groups
# Compute rank of rate within each conc group
transform(Puromycin, rank_rate = ave(rate, conc, FUN = rank))
##    conc rate     state rank_rate
## 1  0.02   76   treated         4
## 2  0.02   47   treated         1
## 3  0.06   97   treated         3
## 4  0.06  107   treated         4
## 5  0.11  123   treated         3
## 6  0.11  139   treated         4
## 7  0.22  159   treated         4
## 8  0.22  152   treated         3
## 9  0.56  191   treated         3
## 10 0.56  201   treated         4
## 11 1.10  207   treated         3
## 12 1.10  200   treated         2
## 13 0.02   67 untreated         3
## 14 0.02   51 untreated         2
## 15 0.06   84 untreated         1
## 16 0.06   86 untreated         2
## 17 0.11   98 untreated         1
## 18 0.11  115 untreated         2
## 19 0.22  131 untreated         2
## 20 0.22  124 untreated         1
## 21 0.56  144 untreated         1
## 22 0.56  158 untreated         2
## 23 1.10  160 untreated         1
ave() computes a group-wise statistic and recycles it back to the original row count, making it the base R equivalent of mutate() after group_by().
ave() + subsetting – Subset rows within groups
# Keep the row(s) with the top rate within each conc group
Puromycin[Puromycin$rate == ave(Puromycin$rate, Puromycin$conc, FUN = max), ]
##    conc rate   state
## 1  0.02   76 treated
## 4  0.06  107 treated
## 6  0.11  139 treated
## 7  0.22  159 treated
## 10 0.56  201 treated
## 11 1.10  207 treated
Allows row selection within each group by comparing each row’s value to a group-wise statistic computed with ave().

🔗 Joins & Bindings

⬆️ Back to Summary

💡 This section lists core joining operations in base R. Click on the triangles to expand.

merge() – A join combines rows from two data frames based on a common key.
# Create a copy of puromycin
puromycin <- Puromycin
head(puromycin)
##   conc rate   state
## 1 0.02   76 treated
## 2 0.02   47 treated
## 3 0.06   97 treated
## 4 0.06  107 treated
## 5 0.11  123 treated
## 6 0.11  139 treated
# Create a second dataset with additional info
treatment_info <- data.frame(
  state = c("treated", "untreated"),
  description = c("Received Puromycin", "Control group")
)
treatment_info
##       state        description
## 1   treated Received Puromycin
## 2 untreated      Control group
# Left join: add treatment descriptions
merge(puromycin, treatment_info, by = "state", all.x = TRUE, sort = FALSE)
##        state conc rate        description
## 1    treated 0.02   76 Received Puromycin
## 2    treated 0.02   47 Received Puromycin
## 3    treated 0.06   97 Received Puromycin
## 4    treated 0.06  107 Received Puromycin
## 5    treated 0.11  123 Received Puromycin
## 6    treated 0.11  139 Received Puromycin
## 7    treated 0.22  159 Received Puromycin
## 8    treated 0.22  152 Received Puromycin
## 9    treated 0.56  191 Received Puromycin
## 10   treated 0.56  201 Received Puromycin
## 11   treated 1.10  207 Received Puromycin
## 12   treated 1.10  200 Received Puromycin
## 13 untreated 0.02   67      Control group
## 14 untreated 0.02   51      Control group
## 15 untreated 0.06   84      Control group
## 16 untreated 0.06   86      Control group
## 17 untreated 0.11   98      Control group
## 18 untreated 0.11  115      Control group
## 19 untreated 0.22  131      Control group
## 20 untreated 0.22  124      Control group
## 21 untreated 0.56  144      Control group
## 22 untreated 0.56  158      Control group
## 23 untreated 1.10  160      Control group

Thus:

  • merge(puromycin, treatment_info, by = "state", all.x = TRUE) merges the description column into the puromycin dataset by matching the state column.
  • all.x = TRUE keeps every row in puromycin (a left join); without it, merge() would default to an inner join.
rbind() / cbind() – Binding combines data frames either by rows (rbind()) or by columns (cbind()).
# Split Puromycin into two groups
treated <- subset(puromycin, state == "treated")
untreated <- subset(puromycin, state == "untreated")

treated # Show treated, 12 rows
##    conc rate   state
## 1  0.02   76 treated
## 2  0.02   47 treated
## 3  0.06   97 treated
## 4  0.06  107 treated
## 5  0.11  123 treated
## 6  0.11  139 treated
## 7  0.22  159 treated
## 8  0.22  152 treated
## 9  0.56  191 treated
## 10 0.56  201 treated
## 11 1.10  207 treated
## 12 1.10  200 treated
# Row binding: recombine after filtering
rbind(treated, untreated) # now 23 rows
##    conc rate     state
## 1  0.02   76   treated
## 2  0.02   47   treated
## 3  0.06   97   treated
## 4  0.06  107   treated
## 5  0.11  123   treated
## 6  0.11  139   treated
## 7  0.22  159   treated
## 8  0.22  152   treated
## 9  0.56  191   treated
## 10 0.56  201   treated
## 11 1.10  207   treated
## 12 1.10  200   treated
## 13 0.02   67 untreated
## 14 0.02   51 untreated
## 15 0.06   84 untreated
## 16 0.06   86 untreated
## 17 0.11   98 untreated
## 18 0.11  115 untreated
## 19 0.22  131 untreated
## 20 0.22  124 untreated
## 21 0.56  144 untreated
## 22 0.56  158 untreated
## 23 1.10  160 untreated
# Column binding: add an extra column
extra_info <- data.frame(source = rep("Lab A", nrow(puromycin)))
extra_info
##    source
## 1   Lab A
## 2   Lab A
## 3   Lab A
## 4   Lab A
## 5   Lab A
## 6   Lab A
## 7   Lab A
## 8   Lab A
## 9   Lab A
## 10  Lab A
## 11  Lab A
## 12  Lab A
## 13  Lab A
## 14  Lab A
## 15  Lab A
## 16  Lab A
## 17  Lab A
## 18  Lab A
## 19  Lab A
## 20  Lab A
## 21  Lab A
## 22  Lab A
## 23  Lab A
cbind(puromycin, extra_info) # Columns bound
##    conc rate     state source
## 1  0.02   76   treated  Lab A
## 2  0.02   47   treated  Lab A
## 3  0.06   97   treated  Lab A
## 4  0.06  107   treated  Lab A
## 5  0.11  123   treated  Lab A
## 6  0.11  139   treated  Lab A
## 7  0.22  159   treated  Lab A
## 8  0.22  152   treated  Lab A
## 9  0.56  191   treated  Lab A
## 10 0.56  201   treated  Lab A
## 11 1.10  207   treated  Lab A
## 12 1.10  200   treated  Lab A
## 13 0.02   67 untreated  Lab A
## 14 0.02   51 untreated  Lab A
## 15 0.06   84 untreated  Lab A
## 16 0.06   86 untreated  Lab A
## 17 0.11   98 untreated  Lab A
## 18 0.11  115 untreated  Lab A
## 19 0.22  131 untreated  Lab A
## 20 0.22  124 untreated  Lab A
## 21 0.56  144 untreated  Lab A
## 22 0.56  158 untreated  Lab A
## 23 1.10  160 untreated  Lab A

Thus:
- rbind() is for stacking datasets vertically (more rows).
- cbind() is for combining datasets horizontally (more columns).


🔄 Reshaping Data

⬆️ Back to Summary

💡 This section lists restructure operations of data between long and wide formats in base R. Click on the triangles to expand.

reshape() to wide – Spread key-value pairs across multiple columns. This is useful when you want to separate values into distinct columns.

First create an extra column with replicates:

Puromycin_reps <- Puromycin
Puromycin_reps$replicate <- rep(1:2, length.out = nrow(Puromycin_reps))
Puromycin_reps
##    conc rate     state replicate
## 1  0.02   76   treated         1
## 2  0.02   47   treated         2
## 3  0.06   97   treated         1
## 4  0.06  107   treated         2
## 5  0.11  123   treated         1
## 6  0.11  139   treated         2
## 7  0.22  159   treated         1
## 8  0.22  152   treated         2
## 9  0.56  191   treated         1
## 10 0.56  201   treated         2
## 11 1.10  207   treated         1
## 12 1.10  200   treated         2
## 13 0.02   67 untreated         1
## 14 0.02   51 untreated         2
## 15 0.06   84 untreated         1
## 16 0.06   86 untreated         2
## 17 0.11   98 untreated         1
## 18 0.11  115 untreated         2
## 19 0.22  131 untreated         1
## 20 0.22  124 untreated         2
## 21 0.56  144 untreated         1
## 22 0.56  158 untreated         2
## 23 1.10  160 untreated         1

Now make the data wide:

Puromycin_wide <- reshape(Puromycin_reps, idvar = c("conc", "state"),
                           timevar = "replicate", direction = "wide")
Puromycin_wide
##    conc     state rate.1 rate.2
## 1  0.02   treated     76     47
## 3  0.06   treated     97    107
## 5  0.11   treated    123    139
## 7  0.22   treated    159    152
## 9  0.56   treated    191    201
## 11 1.10   treated    207    200
## 13 0.02 untreated     67     51
## 15 0.06 untreated     84     86
## 17 0.11 untreated     98    115
## 19 0.22 untreated    131    124
## 21 0.56 untreated    144    158
## 23 1.10 untreated    160     NA

Note that base R’s reshape() names the new columns by pasting the value column with the timevar (e.g. rate.1, rate.2), whereas pivot_wider() names them directly after the replicate values (1, 2). The underlying restructuring is the same.

reshape() to long – Convert columns into key-value pairs. This is useful when you want to gather multiple columns into a single column.
Puromycin_long <- reshape(Puromycin_wide, direction = "long")
Puromycin_long
##                  conc     state replicate rate.1
## 0.02.treated.1   0.02   treated         1     76
## 0.06.treated.1   0.06   treated         1     97
## 0.11.treated.1   0.11   treated         1    123
## 0.22.treated.1   0.22   treated         1    159
## 0.56.treated.1   0.56   treated         1    191
## 1.1.treated.1    1.10   treated         1    207
## 0.02.untreated.1 0.02 untreated         1     67
## 0.06.untreated.1 0.06 untreated         1     84
## 0.11.untreated.1 0.11 untreated         1     98
## 0.22.untreated.1 0.22 untreated         1    131
## 0.56.untreated.1 0.56 untreated         1    144
## 1.1.untreated.1  1.10 untreated         1    160
## 0.02.treated.2   0.02   treated         2     47
## 0.06.treated.2   0.06   treated         2    107
## 0.11.treated.2   0.11   treated         2    139
## 0.22.treated.2   0.22   treated         2    152
## 0.56.treated.2   0.56   treated         2    201
## 1.1.treated.2    1.10   treated         2    200
## 0.02.untreated.2 0.02 untreated         2     51
## 0.06.untreated.2 0.06 untreated         2     86
## 0.11.untreated.2 0.11 untreated         2    115
## 0.22.untreated.2 0.22 untreated         2    124
## 0.56.untreated.2 0.56 untreated         2    158
## 1.1.untreated.2  1.10 untreated         2     NA

📈 Visualization

⬆️ Back to Summary

💡 This section shows how to plot with base R graphics. Click on the triangles to expand.

plot() - The function plot() is used to create visualizations of your data.

Base R has no explicit “layer” system like ggplot2 — instead, a plot is built by calling plot() once to set up the canvas, then adding to it with functions like points(), abline(), legend(), and mtext(), or by passing more arguments to plot() directly. In the example below,the Puromycin dataset is used. Each step below redraws the full plot with one more piece added.

Here is a step-by-step example using R code for a scatter plot with a fitted line:

  • Initialization

Sets up the plotting region and axes, with no data drawn yet.

plot(Puromycin$conc, Puromycin$rate, type = "n", xlab = "", ylab = "")

As you can see, no data are plotted yet. An XY-grid is created and the axes are set.

  • Geometry

Adds the visual representation of the data (points for a scatter plot), using a color per state.

col_map <- c(treated = 1, untreated = 2) # default base R colors

plot(Puromycin$conc, Puromycin$rate,
     col = col_map[Puromycin$state],
     pch = 19, cex = 1.5,
     xlab = "", ylab = "")

As you can see, the data is plotted now. Instead of pch = 19, other values can be used for other point shapes, and lines() or barplot() can be used for other plot types.

  • Statistics

Adds a statistical summary (e.g., a line fit to the data, one per group). For simplicity, we just use a linear model.

col_map <- c(treated = 1, untreated = 2)

plot(Puromycin$conc, Puromycin$rate,
     col = col_map[Puromycin$state],
     pch = 19, cex = 1.5,
     xlab = "", ylab = "")

for (s in names(col_map)) {
  sub <- subset(Puromycin, state == s)
  fit <- lm(rate ~ conc, data = sub)
  abline(fit, col = col_map[s], lwd = 2)
}

Note that for plotting enzyme activity, we normally use the Michaelis-Menten model. This requires more complexity and is omitted in this example. In this case, we used a linear model fit per group with abline(), for demonstration purposes.

  • Scale

Controls the mapping from the data values to the visual properties (e.g., color), made explicit here with a custom color vector and legend().

col_map <- c(treated = "purple", untreated = "darkgreen")

plot(Puromycin$conc, Puromycin$rate,
     col = col_map[Puromycin$state],
     pch = 19, cex = 1.5,
     xlab = "", ylab = "")

for (s in names(col_map)) {
  sub <- subset(Puromycin, state == s)
  fit <- lm(rate ~ conc, data = sub)
  abline(fit, col = col_map[s], lwd = 2)
}

legend("bottomright", legend = names(col_map), col = col_map, pch = 19, title = "Enzyme State")

  • Labels

Adds non-data annotations like titles, axis labels, and a caption, using main/xlab/ylab and mtext().

col_map <- c(treated = "purple", untreated = "darkgreen")

plot(Puromycin$conc, Puromycin$rate,
     col = col_map[Puromycin$state],
     pch = 19, cex = 1.5,
     main = "Reaction Rate vs. Substrate Concentration in Puromycin",
     xlab = "Concentration (ppm)",
     ylab = "Reaction Rate (counts/min)")

for (s in names(col_map)) {
  sub <- subset(Puromycin, state == s)
  fit <- lm(rate ~ conc, data = sub)
  abline(fit, col = col_map[s], lwd = 2)
}

mtext("Effect of enzyme state (treated/untreated)", side = 3, line = 0.5, cex = 0.9)
legend("bottomright", legend = names(col_map), col = col_map, pch = 19, title = "Enzyme State")
mtext("Data from Puromycin dataset", side = 1, line = 4, adj = 1, cex = 0.7)

  • Theme

Controls the non-data graphical elements (e.g., background, grid lines, font size), set globally with par() and grid().

col_map <- c(treated = "purple", untreated = "darkgreen")

par(bg = "white", family = "sans", cex.main = 1.2, cex.lab = 1.05, cex.axis = 0.9)

plot(Puromycin$conc, Puromycin$rate,
     col = col_map[Puromycin$state],
     pch = 19, cex = 1.5,
     main = "Reaction Rate vs. Substrate Concentration in Puromycin",
     xlab = "Concentration (ppm)",
     ylab = "Reaction Rate (counts/min)",
     panel.first = grid(col = "grey90"))

for (s in names(col_map)) {
  sub <- subset(Puromycin, state == s)
  fit <- lm(rate ~ conc, data = sub)
  abline(fit, col = col_map[s], lwd = 2)
}

mtext("Effect of enzyme state (treated/untreated)", side = 3, line = 0.5, cex = 0.9)
legend("bottomright", legend = names(col_map), col = col_map, pch = 19, title = "Enzyme State")
mtext("Data from Puromycin dataset", side = 1, line = 4, adj = 1, cex = 0.7)

This is just a demonstration of the use of the various layers.
For a complete overview of base R graphics see this link.

You can also download a pdf cheatsheet here.


🧰 Utilities

⬆️ Back to Summary

💡 This section lists some utility operations in base R. Click on the triangles to expand.

str() - The function str() is great for quick inspection of your data. Helps you verify column types and spot missing or unexpected values.
str(Puromycin)
## 'data.frame':    23 obs. of  3 variables:
##  $ conc : num  0.02 0.02 0.06 0.06 0.11 0.11 0.22 0.22 0.56 0.56 ...
##  $ rate : num  76 47 97 107 123 139 159 152 191 201 ...
##  $ state: Factor w/ 2 levels "treated","untreated": 1 1 1 1 1 1 1 1 1 1 ...
##  - attr(*, "reference")= chr "A1.3, p. 269"
$ / [[ ]] - Used to extract a single column from a data frame as a vector.
Puromycin$rate
##  [1]  76  47  97 107 123 139 159 152 191 201 207 200  67  51  84  86  98 115 131
## [20] 124 144 158 160
table() - The function table() is used to tally the number of occurrences of values in one or more columns.
table(Puromycin$state)
## 
##   treated untreated 
##        12        11

Go back to the main page
Go back to the R overview page
⬆️ Back to Top


This web page is distributed under the terms of the Creative Commons Attribution License which permits unrestricted use, distribution, and reproduction in any medium, provided the original author and source are credited. Creative Commons License: CC BY-SA 4.0.