
Data Wrangling with R
Preface

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Structure of the book
Chapters @import-data-in-r-basics and @import-data-in-r-advanced focus on reading data from flat/delimited files and spreadsheets. Chapters @dplyr-basics, @joining-tables-in-r-dplyr and @dplyr-helper-functions focus on wrangling data using the dplyr package, chaining verbs with the base R pipe |> (see the primer below). Chapter @r-pipe-magrittr covers the pipe mechanic in detail, including the _ placeholder, anonymous functions with \(x), and reading older magrittr code. Chapter @tibbles-in-r explores tibble(), an alternative for data.frame(). Chapter @tidying-data-with-tidyr reshapes data with the tidyr package (pivots, splitting/combining columns, missing-value helpers). Chapters @strings-in-r, @date-and-time-in-r and @categorical-data-in-r explore ways to handle text, date/time and categorical data.
The pipe |> in 30 seconds
Every example from @dplyr-basics onward chains verbs with the base R pipe |>, available in R 4.1 and later with no extra package:
|>forwards the value on its left into the first argument of the function on its right, soecom |> filter(purchase)meansfilter(ecom, purchase).- Every dplyr verb takes a data frame (or tibble) first and returns one, so a chain like
ecom |> filter(purchase) |> summarise(...)reads top to bottom as “takeecom, keep purchasers, then summarise”.
If you inherit code using the older magrittr pipe %>%, read it the same way: for first-argument forwarding it behaves identically to |>. The differences (_ placeholder, \(x) functions, exposition/assignment variants) are covered in @r-pipe-magrittr.
Software information
This book requires R >= 4.1 (for the native pipe |>) and dplyr >= 1.1.0 (for .by, slice_*() and join_by()); package versions are pinned in renv.lock. The R session information when compiling this book is shown below:
sessionInfo()R version 4.5.2 (2025-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)
Matrix products: default
LAPACK version 3.12.1
locale:
[1] LC_COLLATE=English_India.utf8 LC_CTYPE=English_India.utf8
[3] LC_MONETARY=English_India.utf8 LC_NUMERIC=C
[5] LC_TIME=English_India.utf8
time zone: Asia/Calcutta
tzcode source: internal
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] htmlwidgets_1.6.4 compiler_4.5.2 fastmap_1.2.0 cli_3.6.5
[5] tools_4.5.2 htmltools_0.5.9 yaml_2.3.12 rmarkdown_2.30
[9] knitr_1.50 jsonlite_2.0.0 xfun_0.61 digest_0.6.39
[13] rlang_1.2.0 evaluate_1.0.5
We do not add prompts (> and +) to R source code in this book, and we comment out the text output with two hashes ## by default, as you can see from the R session information above. This is for your convenience when you want to copy and run the code (the text output will be ignored since it is commented out). Package names are in bold text (e.g., rmarkdown), and function names are followed by parentheses (e.g., bookdown::render_book()). The double-colon operator :: means accessing an object from a package.