Start here, even if you have never written a line of code
This lesson assumes nothing. We will explain what R even is, then build up every core idea (values, the data types, vectors, and the data frame) one small step at a time, with pictures. By the end you will understand the heart of R. Go slowly. You cannot break anything.
What is R, and where do I type it?
R is a programming language built for working with data and statistics: loading a table, calculating, and drawing graphs. You write R inside RStudio, a free program with two places to type: the Console (type a line, press Enter, see the answer immediately) and a Script (a saved file of lines you run when you choose, so you keep a record of what you did).
Anything after a # is a comment: a note for humans that R ignores.
2 + 2 # type this in the Console, press Enter -> 4 # this whole line is a comment, R skips it
1. Storing a value: the assignment arrow
To keep a value for later, store it in a name with the assignment arrow <-. Think of the name as a labelled box. (RStudio types the arrow for you with Alt and the minus key.)
gc holds 41.5.gc <- 41.5 # put 41.5 into a box called gc gc # type the name to see what is inside -> 41.5 gc * 2 # use it in a calculation -> 83
2. The core data types
Every value in R has a type: what kind of thing it is. Three basic ones cover most of your early work. Do not memorize this; it is here to come back to.
| Type | What it is | Example |
|---|---|---|
| numeric | A number (whole or decimal). | 41.5 |
| character | Text, always written in quotes. | "TP53" |
| logical | A yes/no value: only TRUE or FALSE. | TRUE |
You usually get a logical by asking a question (a comparison): == equal, != different, > greater, < less.
gc > 50 # is gc greater than 50? -> FALSE "ATG" == "ATG" # are these equal? -> TRUE
One arrow or two equals?
<- stores a value in a name. == (two equals signs) asks "are these equal?" and answers TRUE or FALSE. They are different jobs.
3. Vectors: R's basic building block
R rarely deals with one value at a time. A vector is an ordered set of values of the same type, built with c() ("combine"). Almost every R operation works on a whole vector at once.
expr[1] is 12.4.expr <- c(12.4, 8.1, 15.7, 3.3, 9.9) mean(expr) # average of all five -> 9.88 expr * 2 # doubles EVERY value at once expr[1] # the FIRST value -> 12.4
R counts from 1
Unlike many languages, R numbers positions starting at 1, not 0. So expr[1] is the first value. If you have seen Python, this is the opposite, and a common thing to trip on.
4. Data frames: the spreadsheet of R
A data frame is a table: each column is a variable, each row is one observation. Gene tables, sample sheets, and clinical data all have this shape.
genes <- data.frame( gene = c("TP53", "BRCA1", "EGFR"), length_kb = c(19.1, 81.2, 188.3), chromosome = c("17", "17", "7") ) head(genes) # show the first rows genes$length_kb # pull out one column by name with $
5. The tidyverse: five verbs that do most of the work
The tidyverse is a popular set of R packages for data work. Five verbs cover the vast majority of it. The two you will reach for first are filter (keep rows) and select (keep columns). The picture makes the difference obvious.
mutate adds a column, summarise collapses to a summary, group_by does it per group.library(tidyverse) genes |> filter(length_kb > 50) |> # keep rows where the gene is long arrange(desc(length_kb)) # sort, longest first
The pipe |>
The pipe passes the result of one step into the next, so you read top to bottom as a sentence: "take genes, then filter, then sort." It is the backbone of tidyverse code.
6. Your first plot with ggplot2
ggplot2 builds a graph in layers: start with the data, say what goes on each axis (the aesthetics, aes), then add a geom, the kind of mark to draw.
ggplot(genes, aes(x = gene, y = length_kb)) + geom_col(fill = "#5b9cf8") + labs(title = "Gene lengths", y = "Length (kb)") + theme_minimal()
7. Reading your own data
Real data usually arrives as a CSV file. Reading it is one line.
my_data <- read_csv("my_file.csv") glimpse(my_data) # a quick look at the columns and their types
Every key word, in plain English
- assignment arrow (<-)
- stores a value in a named box.
- numeric
- a number, whole or decimal.
- character
- text, written in quotes.
- logical
- a TRUE or FALSE value.
- vector
- an ordered set of values of the same type, made with
c(). - data frame
- a table: columns are variables, rows are observations.
- $
- pulls one column out of a data frame by name.
- tidyverse
- a set of R packages for tidy data work.
- pipe (|>)
- passes a result into the next step, read as "and then".
- filter / select
- keep certain rows / keep certain columns.
- ggplot2
- the package for building graphs in layers.
- geom
- the kind of mark a plot draws (bars, points, lines).
- comment
- a note after
#that R ignores.
Check yourself
R and the terminal do not run in this page, so here is what correct output looks like. Type each one and compare.
| You type | You should see |
|---|---|
| mean(c(12.4, 8.1, 15.7, 3.3, 9.9)) | 9.88 |
| expr["EGFR"] | 15.7 |
| nrow(genes) | 3 (three rows) |
Check your understanding
|> do?$ selects a named column from a data frame (or a named element from a list).genes and want only the rows on chromosome 17. Which line is correct?filter() keeps rows by a condition, and a condition uses two equals signs (==). select() is for columns, and a single = would be an assignment.gc <- 41.5 do?<- assigns: it puts the value on the right into the name on the left.expr[1]?expr[1] is the first element.filter() and select()?Project: explore a real dataset with R →
Ready for more? R Level 2 (Intermediate) builds straight on this.
