R Intermediate Course 2026

2026-09-24

Slides & Code

  • [f] Full screen
  • [o] Slide Overview
  • [c] Notes
  • [h] help

git repo

R-Intermediate


Clone repo

git clone https://github.com/CECADBioinformaticsCoreFacility/Intermediate_R_Course_2026.git


Slides Directly

https://cecadbioinformaticscorefacility.github.io/Intermediate_R_Course_2026/

Session 2 :: Data Wrangling

From Base R to Tidyverse

You already know how to work with data in R. In the beginner course, we learned:

  • Objects and data types
  • Vectors, matrices and data frames
  • [], [[]] and $
  • Conditional statements
  • Loops and apply()
  • Basic data manipulation
    • reshape()
    • stack() / unstack()
    • cbind() / rbind()
    • merge()

Now we ask a different question:

How can we express data analysis in a clear, readable and reproducible way?

Enter the tidyverse →

Tidyverse

tidyverse is an ecosystem of packages that work together to make data science easier and more efficient in R.

Some of the most popular packages in the tidyverse include:

  • readr for data import
  • tidyr for data tidying
  • tibble for enhanced data frames
  • dplyr for data manipulation
  • stringr for string manipulation
  • forcats for working with categorical variables (factors)
  • lubridate for working with dates and times
  • ggplot2 for data visualization
  • purrr for functional programming

Why Tidyverse for Bioinformatics?

Real biological datasets are rarely ready for analysis.

You constantly need to:

  • Select samples
  • Filter genes
  • Create annotations
  • Combine metadata
  • Summarize replicates
  • Reshape tables
  • Join results from different analyses

Tidyverse gives us a common language for manipulating biological data.

Don’t Forget Base R!

Tidyverse is not a replacement for Base R

You will encounter both styles in real-world R code.

Understanding Base R makes you a better Tidyverse programmer.

tidy data

Tidy data 1 is a standardized way of organizing data values within a dataset. In tidy data:

  • Each variable forms a column.
  • Each observation forms a row.
  • Each type of observational unit forms a table.

This structure makes it easier to manipulate, analyze, and visualize data.

Why readr?

Problem at hand

Getting your data into R is part of the analysis

Base R:

df <- read.csv("rna_results.csv")

readr:

library(readr)

df <- read_csv("rna_results.csv")

Common files:

  • CSV -> comma-separated read_csv(), readr::read_csv_chunked()
  • TSV -> tab-separated read_tsv(),read_tsv_chunked()
  • TXT -> delimited text read_delim()

Why use readr?

  • Fast for large files; One can use chunks or multi threads
  • Better parsing and type detection
  • Clear messages about how columns were interpreted
  • Consistent with the tidyverse
  • Produces tibbles that work naturally with tidyverse tools

readr makes importing and exporting rectangular data predictable and transparent.

Why tibble?

iris
    Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
1            5.1         3.5          1.4         0.2     setosa
2            4.9         3.0          1.4         0.2     setosa
3            4.7         3.2          1.3         0.2     setosa
4            4.6         3.1          1.5         0.2     setosa
5            5.0         3.6          1.4         0.2     setosa
6            5.4         3.9          1.7         0.4     setosa
7            4.6         3.4          1.4         0.3     setosa
8            5.0         3.4          1.5         0.2     setosa
9            4.4         2.9          1.4         0.2     setosa
10           4.9         3.1          1.5         0.1     setosa
11           5.4         3.7          1.5         0.2     setosa
12           4.8         3.4          1.6         0.2     setosa
13           4.8         3.0          1.4         0.1     setosa
14           4.3         3.0          1.1         0.1     setosa
15           5.8         4.0          1.2         0.2     setosa
16           5.7         4.4          1.5         0.4     setosa
17           5.4         3.9          1.3         0.4     setosa
18           5.1         3.5          1.4         0.3     setosa
19           5.7         3.8          1.7         0.3     setosa
20           5.1         3.8          1.5         0.3     setosa
21           5.4         3.4          1.7         0.2     setosa
22           5.1         3.7          1.5         0.4     setosa
23           4.6         3.6          1.0         0.2     setosa
24           5.1         3.3          1.7         0.5     setosa
25           4.8         3.4          1.9         0.2     setosa
26           5.0         3.0          1.6         0.2     setosa
27           5.0         3.4          1.6         0.4     setosa
28           5.2         3.5          1.5         0.2     setosa
29           5.2         3.4          1.4         0.2     setosa
30           4.7         3.2          1.6         0.2     setosa
31           4.8         3.1          1.6         0.2     setosa
32           5.4         3.4          1.5         0.4     setosa
33           5.2         4.1          1.5         0.1     setosa
34           5.5         4.2          1.4         0.2     setosa
35           4.9         3.1          1.5         0.2     setosa
36           5.0         3.2          1.2         0.2     setosa
37           5.5         3.5          1.3         0.2     setosa
38           4.9         3.6          1.4         0.1     setosa
39           4.4         3.0          1.3         0.2     setosa
40           5.1         3.4          1.5         0.2     setosa
41           5.0         3.5          1.3         0.3     setosa
42           4.5         2.3          1.3         0.3     setosa
43           4.4         3.2          1.3         0.2     setosa
44           5.0         3.5          1.6         0.6     setosa
45           5.1         3.8          1.9         0.4     setosa
46           4.8         3.0          1.4         0.3     setosa
47           5.1         3.8          1.6         0.2     setosa
48           4.6         3.2          1.4         0.2     setosa
49           5.3         3.7          1.5         0.2     setosa
50           5.0         3.3          1.4         0.2     setosa
51           7.0         3.2          4.7         1.4 versicolor
52           6.4         3.2          4.5         1.5 versicolor
53           6.9         3.1          4.9         1.5 versicolor
54           5.5         2.3          4.0         1.3 versicolor
55           6.5         2.8          4.6         1.5 versicolor
56           5.7         2.8          4.5         1.3 versicolor
57           6.3         3.3          4.7         1.6 versicolor
58           4.9         2.4          3.3         1.0 versicolor
59           6.6         2.9          4.6         1.3 versicolor
60           5.2         2.7          3.9         1.4 versicolor
61           5.0         2.0          3.5         1.0 versicolor
62           5.9         3.0          4.2         1.5 versicolor
63           6.0         2.2          4.0         1.0 versicolor
64           6.1         2.9          4.7         1.4 versicolor
65           5.6         2.9          3.6         1.3 versicolor
66           6.7         3.1          4.4         1.4 versicolor
67           5.6         3.0          4.5         1.5 versicolor
68           5.8         2.7          4.1         1.0 versicolor
69           6.2         2.2          4.5         1.5 versicolor
70           5.6         2.5          3.9         1.1 versicolor
71           5.9         3.2          4.8         1.8 versicolor
72           6.1         2.8          4.0         1.3 versicolor
73           6.3         2.5          4.9         1.5 versicolor
74           6.1         2.8          4.7         1.2 versicolor
75           6.4         2.9          4.3         1.3 versicolor
76           6.6         3.0          4.4         1.4 versicolor
77           6.8         2.8          4.8         1.4 versicolor
78           6.7         3.0          5.0         1.7 versicolor
79           6.0         2.9          4.5         1.5 versicolor
80           5.7         2.6          3.5         1.0 versicolor
81           5.5         2.4          3.8         1.1 versicolor
82           5.5         2.4          3.7         1.0 versicolor
83           5.8         2.7          3.9         1.2 versicolor
84           6.0         2.7          5.1         1.6 versicolor
85           5.4         3.0          4.5         1.5 versicolor
86           6.0         3.4          4.5         1.6 versicolor
87           6.7         3.1          4.7         1.5 versicolor
88           6.3         2.3          4.4         1.3 versicolor
89           5.6         3.0          4.1         1.3 versicolor
90           5.5         2.5          4.0         1.3 versicolor
91           5.5         2.6          4.4         1.2 versicolor
92           6.1         3.0          4.6         1.4 versicolor
93           5.8         2.6          4.0         1.2 versicolor
94           5.0         2.3          3.3         1.0 versicolor
95           5.6         2.7          4.2         1.3 versicolor
96           5.7         3.0          4.2         1.2 versicolor
97           5.7         2.9          4.2         1.3 versicolor
98           6.2         2.9          4.3         1.3 versicolor
99           5.1         2.5          3.0         1.1 versicolor
100          5.7         2.8          4.1         1.3 versicolor
101          6.3         3.3          6.0         2.5  virginica
102          5.8         2.7          5.1         1.9  virginica
103          7.1         3.0          5.9         2.1  virginica
104          6.3         2.9          5.6         1.8  virginica
105          6.5         3.0          5.8         2.2  virginica
106          7.6         3.0          6.6         2.1  virginica
107          4.9         2.5          4.5         1.7  virginica
108          7.3         2.9          6.3         1.8  virginica
109          6.7         2.5          5.8         1.8  virginica
110          7.2         3.6          6.1         2.5  virginica
111          6.5         3.2          5.1         2.0  virginica
112          6.4         2.7          5.3         1.9  virginica
113          6.8         3.0          5.5         2.1  virginica
114          5.7         2.5          5.0         2.0  virginica
115          5.8         2.8          5.1         2.4  virginica
116          6.4         3.2          5.3         2.3  virginica
117          6.5         3.0          5.5         1.8  virginica
118          7.7         3.8          6.7         2.2  virginica
119          7.7         2.6          6.9         2.3  virginica
120          6.0         2.2          5.0         1.5  virginica
121          6.9         3.2          5.7         2.3  virginica
122          5.6         2.8          4.9         2.0  virginica
123          7.7         2.8          6.7         2.0  virginica
124          6.3         2.7          4.9         1.8  virginica
125          6.7         3.3          5.7         2.1  virginica
126          7.2         3.2          6.0         1.8  virginica
127          6.2         2.8          4.8         1.8  virginica
128          6.1         3.0          4.9         1.8  virginica
129          6.4         2.8          5.6         2.1  virginica
130          7.2         3.0          5.8         1.6  virginica
131          7.4         2.8          6.1         1.9  virginica
132          7.9         3.8          6.4         2.0  virginica
133          6.4         2.8          5.6         2.2  virginica
134          6.3         2.8          5.1         1.5  virginica
135          6.1         2.6          5.6         1.4  virginica
136          7.7         3.0          6.1         2.3  virginica
137          6.3         3.4          5.6         2.4  virginica
138          6.4         3.1          5.5         1.8  virginica
139          6.0         3.0          4.8         1.8  virginica
140          6.9         3.1          5.4         2.1  virginica
141          6.7         3.1          5.6         2.4  virginica
142          6.9         3.1          5.1         2.3  virginica
143          5.8         2.7          5.1         1.9  virginica
144          6.8         3.2          5.9         2.3  virginica
145          6.7         3.3          5.7         2.5  virginica
146          6.7         3.0          5.2         2.3  virginica
147          6.3         2.5          5.0         1.9  virginica
148          6.5         3.0          5.2         2.0  virginica
149          6.2         3.4          5.4         2.3  virginica
150          5.9         3.0          5.1         1.8  virginica
tibble::tibble(iris)
# A tibble: 150 × 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1          5.1         3.5          1.4         0.2 setosa 
 2          4.9         3            1.4         0.2 setosa 
 3          4.7         3.2          1.3         0.2 setosa 
 4          4.6         3.1          1.5         0.2 setosa 
 5          5           3.6          1.4         0.2 setosa 
 6          5.4         3.9          1.7         0.4 setosa 
 7          4.6         3.4          1.4         0.3 setosa 
 8          5           3.4          1.5         0.2 setosa 
 9          4.4         2.9          1.4         0.2 setosa 
10          4.9         3.1          1.5         0.1 setosa 
# ℹ 140 more rows

tibbles are designed to make working with data safer and easier to inspect.

Note

You immediately see:

  1. Number of rows
  2. Number of columns
  3. Column names
  4. Column types
  5. A preview of the data

Why use tibble

  1. A tibble is a data frame.
  2. tibbles print data in a human-friendly way.
  3. tibbles are stricter and more predictable in some situations.
  4. tibbles are the natural data object of the tidyverse.

Don’t think of tibble as another complicated thing to learn. Think of it as the tidyverse’s modern data frame.

Why tidyr?

Real-world data are often not in the right shape. Biological data frequently come in formats that are:

  • Wide when we need long
  • Long when we need wide
  • Spread across multiple columns
  • Packed with multiple values in one column

Wide → Long

iris_filtered <- iris |>
  filter(Sepal.Length > 7) |>
  mutate(ID = row_number())

 iris_filtered
   Sepal.Length Sepal.Width Petal.Length Petal.Width   Species ID
1           7.1         3.0          5.9         2.1 virginica  1
2           7.6         3.0          6.6         2.1 virginica  2
3           7.3         2.9          6.3         1.8 virginica  3
4           7.2         3.6          6.1         2.5 virginica  4
5           7.7         3.8          6.7         2.2 virginica  5
6           7.7         2.6          6.9         2.3 virginica  6
7           7.7         2.8          6.7         2.0 virginica  7
8           7.2         3.2          6.0         1.8 virginica  8
9           7.2         3.0          5.8         1.6 virginica  9
10          7.4         2.8          6.1         1.9 virginica 10
11          7.9         3.8          6.4         2.0 virginica 11
12          7.7         3.0          6.1         2.3 virginica 12

Why tidyr?

Real-world data are often not in the right shape. Biological data frequently come in formats that are:

  • Wide when we need long
  • Long when we need wide
  • Spread across multiple columns
  • Packed with multiple values in one column

Wide → Long

iris_filtered <- iris |>
  filter(Sepal.Length > 7) |>
  mutate(ID = row_number())

 iris_filtered
   Sepal.Length Sepal.Width Petal.Length Petal.Width   Species ID
1           7.1         3.0          5.9         2.1 virginica  1
2           7.6         3.0          6.6         2.1 virginica  2
3           7.3         2.9          6.3         1.8 virginica  3
4           7.2         3.6          6.1         2.5 virginica  4
5           7.7         3.8          6.7         2.2 virginica  5
6           7.7         2.6          6.9         2.3 virginica  6
7           7.7         2.8          6.7         2.0 virginica  7
8           7.2         3.2          6.0         1.8 virginica  8
9           7.2         3.0          5.8         1.6 virginica  9
10          7.4         2.8          6.1         1.9 virginica 10
11          7.9         3.8          6.4         2.0 virginica 11
12          7.7         3.0          6.1         2.3 virginica 12

Why tidyr?

Real-world data are often not in the right shape. Biological data frequently come in formats that are:

  • Wide when we need long
  • Long when we need wide
  • Spread across multiple columns
  • Packed with multiple values in one column

Wide → Long

long_base <- reshape(
  iris_filtered,
  varying = c(
    "Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"
  ),
  v.names = "Value",
  timevar = "Measurement",
  times = c(
    "Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"
  ),
  idvar = "ID",
  direction = "long"
)

long_base
                  Species ID  Measurement Value
1.Sepal.Length  virginica  1 Sepal.Length   7.1
2.Sepal.Length  virginica  2 Sepal.Length   7.6
3.Sepal.Length  virginica  3 Sepal.Length   7.3
4.Sepal.Length  virginica  4 Sepal.Length   7.2
5.Sepal.Length  virginica  5 Sepal.Length   7.7
6.Sepal.Length  virginica  6 Sepal.Length   7.7
7.Sepal.Length  virginica  7 Sepal.Length   7.7
8.Sepal.Length  virginica  8 Sepal.Length   7.2
9.Sepal.Length  virginica  9 Sepal.Length   7.2
10.Sepal.Length virginica 10 Sepal.Length   7.4
11.Sepal.Length virginica 11 Sepal.Length   7.9
12.Sepal.Length virginica 12 Sepal.Length   7.7
1.Sepal.Width   virginica  1  Sepal.Width   3.0
2.Sepal.Width   virginica  2  Sepal.Width   3.0
3.Sepal.Width   virginica  3  Sepal.Width   2.9
4.Sepal.Width   virginica  4  Sepal.Width   3.6
5.Sepal.Width   virginica  5  Sepal.Width   3.8
6.Sepal.Width   virginica  6  Sepal.Width   2.6
7.Sepal.Width   virginica  7  Sepal.Width   2.8
8.Sepal.Width   virginica  8  Sepal.Width   3.2
9.Sepal.Width   virginica  9  Sepal.Width   3.0
10.Sepal.Width  virginica 10  Sepal.Width   2.8
11.Sepal.Width  virginica 11  Sepal.Width   3.8
12.Sepal.Width  virginica 12  Sepal.Width   3.0
1.Petal.Length  virginica  1 Petal.Length   5.9
2.Petal.Length  virginica  2 Petal.Length   6.6
3.Petal.Length  virginica  3 Petal.Length   6.3
4.Petal.Length  virginica  4 Petal.Length   6.1
5.Petal.Length  virginica  5 Petal.Length   6.7
6.Petal.Length  virginica  6 Petal.Length   6.9
7.Petal.Length  virginica  7 Petal.Length   6.7
8.Petal.Length  virginica  8 Petal.Length   6.0
9.Petal.Length  virginica  9 Petal.Length   5.8
10.Petal.Length virginica 10 Petal.Length   6.1
11.Petal.Length virginica 11 Petal.Length   6.4
12.Petal.Length virginica 12 Petal.Length   6.1
1.Petal.Width   virginica  1  Petal.Width   2.1
2.Petal.Width   virginica  2  Petal.Width   2.1
3.Petal.Width   virginica  3  Petal.Width   1.8
4.Petal.Width   virginica  4  Petal.Width   2.5
5.Petal.Width   virginica  5  Petal.Width   2.2
6.Petal.Width   virginica  6  Petal.Width   2.3
7.Petal.Width   virginica  7  Petal.Width   2.0
8.Petal.Width   virginica  8  Petal.Width   1.8
9.Petal.Width   virginica  9  Petal.Width   1.6
10.Petal.Width  virginica 10  Petal.Width   1.9
11.Petal.Width  virginica 11  Petal.Width   2.0
12.Petal.Width  virginica 12  Petal.Width   2.3
long <- iris_filtered |>
  pivot_longer(
    cols = c(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width),
    names_to = "Measurement",
    values_to = "Value"
  )

long 
# A tibble: 48 × 4
   Species      ID Measurement  Value
   <fct>     <int> <chr>        <dbl>
 1 virginica     1 Sepal.Length   7.1
 2 virginica     1 Sepal.Width    3  
 3 virginica     1 Petal.Length   5.9
 4 virginica     1 Petal.Width    2.1
 5 virginica     2 Sepal.Length   7.6
 6 virginica     2 Sepal.Width    3  
 7 virginica     2 Petal.Length   6.6
 8 virginica     2 Petal.Width    2.1
 9 virginica     3 Sepal.Length   7.3
10 virginica     3 Sepal.Width    2.9
# ℹ 38 more rows

Why tidyr?

Real-world data are often not in the right shape. Biological data frequently come in formats that are:

  • Wide when we need long
  • Long when we need wide
  • Spread across multiple columns
  • Packed with multiple values in one column

Long → Wide

wide_base <- reshape(
  long_base,
  idvar = "ID",
  timevar = "Measurement",
  direction = "wide"
)

wide_base |> head()
               ID Species.Sepal.Length Value.Sepal.Length Species.Sepal.Width
1.Sepal.Length  1            virginica                7.1           virginica
2.Sepal.Length  2            virginica                7.6           virginica
3.Sepal.Length  3            virginica                7.3           virginica
4.Sepal.Length  4            virginica                7.2           virginica
5.Sepal.Length  5            virginica                7.7           virginica
6.Sepal.Length  6            virginica                7.7           virginica
               Value.Sepal.Width Species.Petal.Length Value.Petal.Length
1.Sepal.Length               3.0            virginica                5.9
2.Sepal.Length               3.0            virginica                6.6
3.Sepal.Length               2.9            virginica                6.3
4.Sepal.Length               3.6            virginica                6.1
5.Sepal.Length               3.8            virginica                6.7
6.Sepal.Length               2.6            virginica                6.9
               Species.Petal.Width Value.Petal.Width
1.Sepal.Length           virginica               2.1
2.Sepal.Length           virginica               2.1
3.Sepal.Length           virginica               1.8
4.Sepal.Length           virginica               2.5
5.Sepal.Length           virginica               2.2
6.Sepal.Length           virginica               2.3
wide <- long |>
  pivot_wider(
    names_from = Measurement,
    values_from = Value
  )

wide |> head()
# A tibble: 6 × 6
  Species      ID Sepal.Length Sepal.Width Petal.Length Petal.Width
  <fct>     <int>        <dbl>       <dbl>        <dbl>       <dbl>
1 virginica     1          7.1         3            5.9         2.1
2 virginica     2          7.6         3            6.6         2.1
3 virginica     3          7.3         2.9          6.3         1.8
4 virginica     4          7.2         3.6          6.1         2.5
5 virginica     5          7.7         3.8          6.7         2.2
6 virginica     6          7.7         2.6          6.9         2.3

Why use tidyr?

tidyr changes the shape of your data without changing the information.

The Power of the Pipe |> or %>%

  • |> or %>% operators allow you to pass the output of one function as the input to another function, making your code more readable and concise. Simply saying, chain multiple operations together.

The Power of the Pipe |> or %>%

Consider a typical RNA-Seq analysis:

flowchart TB
  A(RNA-seq results) --> B(Remove missing values)
  B --> C(Keep significant genes)
  C --> D(Classify genes as Up / Down)
  D --> E(Sort by effect size)
  E --> F(Keep relevant columns)

  

res |>
  filter(!is.na(padj),
         padj < 0.05) |>
  mutate(
    direction = if_else(
      log2FoldChange > 0,
      "Up",
      "Down"
    )
  ) |>
  arrange(desc(abs(log2FoldChange))) |>
  select(gene, log2FoldChange, padj, direction)




Note

The code follows the logic of the analysis.

Why dplyr?

A grammar for data manipulation

Instead of thinking about complicated indexing:

df[df$group == "Control", ]

we can describe what we want:

df |>
  filter(group == "Control")

The key idea

Use a small number of verbs to describe what you want to do to your data.

Task dplyr verb
Choose rows filter()
Choose columns select()
Create/change columns mutate()
Summarize summarise()
Group group_by()
Sort arrange()
Combine datasets *_join()

Data Reshaping in Base R vs dplyr

# 1. Filter rows, select columns, and create the new column
sub_iris <- subset(iris, 
  Sepal.Length > 5, 
  select = c(Sepal.Length, Petal.Length, Species)
  )
sub_iris$SePa.Length <- sub_iris$Sepal.Length + sub_iris$Petal.Length

# 2. Sort rows in descending order of the new column
sub_iris <- sub_iris[order(-sub_iris$SePa.Length), ]

# 3. Group by Species and calculate mean and count (n)
result <- aggregate(SePa.Length ~ Species, 
  data = sub_iris, 
  FUN = function(x) {
  c(mean = mean(x), n = length(x))
})

# 4. Flatten the matrix column produced by aggregate into a clean data frame
result <- do.call(data.frame, result)
names(result) <- c("Species", "mean", "n")

print(result)
     Species      mean  n
1     setosa  6.822727 22
2 versicolor 10.314894 47
3  virginica 12.195918 49
library(dplyr)
iris |> 
   filter(Sepal.Length > 5) |>                          # filter rows
   select(Sepal.Length, Petal.Length, Species) |>       # select columns
   mutate(SePa.Length=Sepal.Length+Petal.Length) |>     # add new column
   arrange(desc(SePa.Length)) |>                        # sort rows
   group_by(Species) |>                                 # group by Species
   summarise(mean = mean(SePa.Length), n = n())         # summarise
# A tibble: 3 × 3
  Species     mean     n
  <fct>      <dbl> <int>
1 setosa      6.82    22
2 versicolor 10.3     47
3 virginica  12.2     49

Pick columns using dplyr::select()

base-r

dplyr

select specific columns by name

iris[,c("Sepal.Length", "Sepal.Width")]

select(iris,Sepal.Length, Petal.Length, Species)

select specific columns by position

iris[,c(1,2,3)]

select(iris,1,2,3)

select a range of columns by position

iris[,c(1:3)]

select(iris,1:3)

exclude specific columns by position

iris[,-c(1,2)]

select(iris,-c(4:5))

reorder columns

iris[,c("Species","Sepal.Length", "Petal.Length")]

select(iris,Species, Sepal.Length, Petal.Length)

Note

  • select() provides columns by position, not based on conditions. For More on select() follow `dplyr: select_helpers

Pick rows Using dplyr::slice()

base-r

dplyr

get specific rows

iris[c(1,3,5,7),]

slice(iris,c(1,3,5,7))

get rows by range

iris[10:20,]

slice(iris,10:20)

get first 10 rows

iris[1:10,] head(iris, 10)

slice((1:10) slice_head(iris, n = 10)

get last 10 rows

tail(iris, 10)

slice((n()-9):n()) or slice_tail(iris, n = 10)

Note

  • slice() selects rows by row number, not based on conditions.

Functional row selection with filter()

base-r

dplyr

Select a conditional parameter

iris[iris$Sepal.Length > 7,]

filter(iris,Sepal.Length > 7)

match values in a vector

iris[iris$Sepal.Length %in% c(5.0, 5.1), ]

filter(iris, Sepal.Length %in% c(5.0, 5.1))

remove missing values

iris[is.na(iris$Sepal.Length), ]

filter(iris, is.na(Sepal.Length) )

Select a conditional parameter for abs()

iris[abs(iris$Sepal.Length - 5) < 0.1, ]

filter(iris, abs(Sepal.Length - 5) < 0.1 )

Filter rows base on pattern matching

iris[grepl("ver", iris$Species), ]

filter(iris, grepl("ver", Species))

Note

  • filter() selects rows based on conditions. & and | are not same as && and ||.

mutate() & transmute() to add or transform columns

base-r

dplyr

Add new column

iris$Sepa.Length <- iris$Sepal.Length + iris$Petal.Length

mutate(iris,SePa.Length=Sepal.Length+Petal.Length)

create new column and drop others

with(iris, data.frame(SePa.Length=ifelse(Sepal.Length > 5, "Long", "Short")))

transmute(iris,SePa.Length=ifelse(Sepal.Length > 5, "Long", "Short"))

arrange (sorting)

base-r

dplyr

sort by ascending order

iris[order(iris$Sepal.Length, iris$Petal.Length), ]

arrange(iris, Sepal.Length, Petal.Length)

sort by descending order

iris[order(-iris$Sepal.Length, -iris$Petal.Length), ]

arrange(iris,desc(Sepal.Length), desc(Petal.Length))

group_by + summarise (aggregation)

base-r

dplyr

group by Species and calculate mean and count

aggregate(cbind(Petal.Length, Sepal.Length) ~ Species + Petal.Width, 
data = iris, 
FUN = function(x) { c(mean = mean(x), n = length(x))
})
      Species Petal.Width Petal.Length.mean Petal.Length.n Sepal.Length.mean
1      setosa         0.1          1.380000       5.000000          4.820000
2      setosa         0.2          1.444828      29.000000          4.972414
3      setosa         0.3          1.428571       7.000000          4.971429
4      setosa         0.4          1.571429       7.000000          5.300000
5      setosa         0.5          1.700000       1.000000          5.100000
6      setosa         0.6          1.600000       1.000000          5.000000
7  versicolor         1.0          3.628571       7.000000          5.414286
8  versicolor         1.1          3.566667       3.000000          5.400000
9  versicolor         1.2          4.240000       5.000000          5.780000
10 versicolor         1.3          4.176923      13.000000          5.884615
11 versicolor         1.4          4.500000       7.000000          6.357143
12  virginica         1.4          5.600000       1.000000          6.100000
13 versicolor         1.5          4.580000      10.000000          6.190000
14  virginica         1.5          5.050000       2.000000          6.150000
15 versicolor         1.6          4.766667       3.000000          6.100000
16  virginica         1.6          5.800000       1.000000          7.200000
17 versicolor         1.7          5.000000       1.000000          6.700000
18  virginica         1.7          4.500000       1.000000          4.900000
19 versicolor         1.8          4.800000       1.000000          5.900000
20  virginica         1.8          5.381818      11.000000          6.445455
21  virginica         1.9          5.320000       5.000000          6.340000
22  virginica         2.0          5.550000       6.000000          6.650000
23  virginica         2.1          5.783333       6.000000          6.916667
24  virginica         2.2          6.033333       3.000000          6.866667
25  virginica         2.3          5.700000       8.000000          6.912500
26  virginica         2.4          5.433333       3.000000          6.266667
27  virginica         2.5          5.933333       3.000000          6.733333
   Sepal.Length.n
1        5.000000
2       29.000000
3        7.000000
4        7.000000
5        1.000000
6        1.000000
7        7.000000
8        3.000000
9        5.000000
10      13.000000
11       7.000000
12       1.000000
13      10.000000
14       2.000000
15       3.000000
16       1.000000
17       1.000000
18       1.000000
19       1.000000
20      11.000000
21       5.000000
22       6.000000
23       6.000000
24       3.000000
25       8.000000
26       3.000000
27       3.000000
iris %>%
  group_by(Species,Petal.Width) %>% 
  summarise(mean = mean(Petal.Length), mean2 = mean(Sepal.Length) , n = n()) %>%
  ungroup()
# A tibble: 27 × 5
   Species    Petal.Width  mean mean2     n
   <fct>            <dbl> <dbl> <dbl> <int>
 1 setosa             0.1  1.38  4.82     5
 2 setosa             0.2  1.44  4.97    29
 3 setosa             0.3  1.43  4.97     7
 4 setosa             0.4  1.57  5.3      7
 5 setosa             0.5  1.7   5.1      1
 6 setosa             0.6  1.6   5        1
 7 versicolor         1    3.63  5.41     7
 8 versicolor         1.1  3.57  5.4      3
 9 versicolor         1.2  4.24  5.78     5
10 versicolor         1.3  4.18  5.88    13
# ℹ 17 more rows

distinct (deduplication)

base-r

dplyr

return unique rows

unique(iris["Species"]) or unique(iris$Species)

distinct(iris,Species)

return unique rows based on Index column and keep all other columns

iris[!duplicated(iris$Species), ]

distinct(iris,Species,.keep_all= TRUE)

bind_cols() & bind_rows()

base-r

dplyr

Horizontal Binding

cbind(df_long, df_short)

bind_cols(df_long, df_short)

Vertical Binding

rbind(df1, df2)

bind_rows(df1, df2)

join() function

base-r

dplyr

Left Join

merge(x, y, all.x = TRUE)

left_join(x, y)

Right Join

merge(x, y, all.y = TRUE)

right_join(x, y)

Inner Join

merge(x, y, all = FALSE)

inner_join(x, y)

Full Join

merge(x, y, all = TRUE)

full_join(x, y)

Semi Join

x[x$id %in% y$id, , drop = FALSE]

semi_join(x, y)

Anti Join

x[ !x$id %in% y$id, ]

anti_join(x, y)

flowchart LR
    %% Style Configuration
    classDef importStyle fill:#E3F2FD,stroke:#90CAF9,stroke-width:2px,rx:5px,ry:5px;
    classDef tidyStyle fill:#F3E5F5,stroke:#CE93D8,stroke-width:2px,rx:5px,ry:5px;
    classDef transformStyle fill:#FFF3E0,stroke:#FFCC80,stroke-width:2px,rx:5px,ry:5px;
    classDef visualiseStyle fill:#FFEBEE,stroke:#EF9A9A,stroke-width:2px,rx:5px,ry:5px;
    
    %% Hazy / Inactive Styles (Lower opacity, dashed faded borders)
    classDef hazyModel fill:#E8F5E9,stroke:#A5D6A7,stroke-width:2px,stroke-dasharray: 5 5,rx:5px,ry:5px,opacity:0.9;
    classDef hazyProgram fill:#ECEFF1,stroke:#B0BEC5,stroke-width:2px,stroke-dasharray: 5 5,rx:5px,ry:5px,opacity:0.9;

    %% Main Row Nodes (Active Workflow)
    Import["📥 <b><font color='#1565C0' size=6>Import</font></b><font color='#777777'><br><br><b>readr</b><br>readxl<br>haven<br>httr<br>rvest<br>xml2"</font>]:::importStyle
    Tidy["🧹 <b><font color='#6A1B9A' size=6>Tidy</font></b><font color='#777777'><br><br><b>tibble<br>tidyr</b>"</font>]:::tidyStyle
    Transform["⚙️ <b><font color='#C62828' size=6>Transform</font></b><font color='#777777'><br><br><b>dplyr</b><br>forcats<br>hms<br>lubridate<br>stringr"</font>]:::transformStyle
    Visualise["📊 <b><font color='#E65100' size=6>Visualise</font></b><font color='#777777'><br><br><b>ggplot2</b>"</font>]:::visualiseStyle

    %% Isolated / Hazy Nodes
    Model["🧠 <b><font color='#555555' size=6>Model</font></b><br><br><font color='#777777'>broom<br>modelr</font>"]:::hazyModel
    Program["💻 <b><font color='#555555' size=6>Program</font></b><br><br><font color='#777777'><b>purrr</b><br>magrittr</font>"]:::hazyProgram

    %% Active Row Connection
    Import --> Tidy --> Transform --> Visualise

    %% Hazy Loop Connections for Model (Dashed & Faded)
    Visualise -.-> Model
    Model -.-> Transform
    %% Model -.-> Program
    Transform ~~~ Program

    %% Link styles to explicitly fade the arrow paths
    linkStyle 3 stroke:#999999,stroke-width:1px,stroke-dasharray: 3 5;
    linkStyle 4 stroke:#999999,stroke-width:1px,stroke-dasharray: 3 5;

    %% Something[" <img src='images/file.png' width='5' /> <b> Something</b>"]