R: Level 1 (Basics)

For complete beginners with no coding background. We start from what R even is, then build every core idea (data types, vectors, and the data frame) one small step at a time, with pictures.

🟢 Beginner ⏱️ ~2 hr 📊 R · tidyverse

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

You type R in the Console or a Script R computes runs your line You get a result a number, a table, or a plot
R in one picture: data in, an answer or a graph out.

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
Try it: open RStudio, type a sum in the Console, press Enter. Then change the numbers and try again. That loop (change something, run, see the result) is how you will learn everything here.

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 <- 41.5 41.5 the box is named gc
The arrow points from the value into the box. Here 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.

TypeWhat it isExample
numericA number (whole or decimal).41.5
characterText, always written in quotes."TP53"
logicalA 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.

12.4 8.1 15.7 3.3 9.9 1 2 3 4 5
A vector of expression values. The positions (purple) start at 1, so 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.

gene length_kb chromosome TP53 19.1 17 BRCA1 81.2 17 EGFR 188.3 7 columns = variables · rows = observations
A data frame. The purple header names each column; every row is one gene.
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.

filter() keeps ROWS select() keeps COLUMNS keep the rows that match a condition keep only certain columns
filter works across rows; select works across columns. The other three: 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.

data + aes(x, y)which columns map to axes + geom_col()the kind of mark = a plot
A ggplot is data plus aesthetics plus a geom. Swap the geom and you get a different chart of the same data.
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 typeYou 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

What does the pipe |> do?
Right. Read it as "and then": take the data, then filter, then sort.
How do you pull a single column out of a data frame by name?
Correct. $ selects a named column from a data frame (or a named element from a list).
You have a data frame genes and want only the rows on chromosome 17. Which line is correct?
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.
What does gc <- 41.5 do?
The arrow <- assigns: it puts the value on the right into the name on the left.
In R, what is expr[1]?
R numbers positions starting at 1, so expr[1] is the first element.
What is the difference between filter() and select()?
Rows are observations (filter) and columns are variables (select). Rows = filter, columns = select.
A data frame is best described as...
A data frame is R's spreadsheet: a table of columns (variables) and rows (observations).
Next in the R series, the payoff

Project: explore a real dataset with R →

Ready for more? R Level 2 (Intermediate) builds straight on this.