R for Data Science, 2nd Edition
Ishe Chinyoka
- 13 minutes readTable of Contents
There is a particular kind of book that teaches you a programming language, and another kind that teaches you what to do with that language.
R for Data Science, by Hadley Wickham, Mine Γetinkaya-Rundel, and Garrett Grolemund, belongs firmly in the second category.
It assumes that you already know R. It does not try to teach you what a variable is, how a function works, or how to write your first for loop. Instead, it asks a much more interesting question:
Now that you know R, what can you actually do with data?
That makes it a particularly interesting book for this month’s exploration of data analysis from the terminal. The Textsmith may not spend his days analysing enormous datasets, building predictive models, or maintaining data warehouses. But the underlying skills of getting data into a useful form, cleaning it, transforming it, exploring it, visualising it and communicating what was discovered are valuable far beyond the world of professional data science.
And that is where R for Data Science shines.
The book assumes you already speak R
The first thing to understand about this book is what it is not.
It is not an introduction to R programming.
If you have never written R before, this is probably not where you should begin. The authors assume that you already have enough familiarity with R to follow code and understand the basic mechanics of the language.
That is actually one of the book’s strengths.
Instead of spending hundreds of pages explaining the language, the authors can concentrate on the craft of data analysis. The result is a book that feels less like a programming-language manual and more like a workshop manual for working with information.
This distinction matters.
Learning a language and learning what to do with a language are two different things. You can know R syntax without knowing how to approach a dirty CSV file. You can know functions without knowing which questions to ask of a dataset. You can know how to produce a plot without knowing whether that plot actually tells your reader anything useful.
R for Data Science concentrates on that second layer.
The whole game
One of the most appealing changes in the second edition is the way the book begins.
The first part is called “Whole game”, and the name is wonderfully appropriate.
Rather than throwing the reader immediately into the details of data manipulation, the authors first provide a map of the territory. You see the broad cycle:
Import β Tidy β Transform β Visualize β Understand β Communicate
with programming surrounding the entire process.
That is an important way to think about data analysis.
A dataset does not arrive on your computer as a perfectly organised table waiting for summarize() to be called on it. Real data has to be imported. It has to be inspected. It may need cleaning and restructuring. You have to decide which variables matter. You need to investigate what the data is saying. You may need to transform it repeatedly. Eventually, you need to communicate what you found.
The authors describe this as the “whole game” of data science: enough of every major piece to allow you to tackle real, if relatively simple, datasets before diving deeper into each component.
That philosophy is very close to the way a Textsmith approaches a problem.
We are not interested in a command simply because it exists. We want to understand where it fits in the transformation.
Tidy data: where the real work begins
If there is one idea that has become closely associated with Wickham’s approach to data analysis, it is tidy data.
The principle is deceptively simple: each variable should form a column, each observation should form a row, and each value should occupy a cell.
But the importance of this idea goes far beyond table formatting.
Messy structure makes everything downstream harder.
You cannot easily filter information if the information is stored inconsistently. You cannot reliably summarise it if what should be one variable has been scattered across several columns. You cannot easily visualise it if the structure of the dataset fights the tools you want to use.
Tidying therefore becomes a form of preparation for thought.
This is something that anyone who works with text files from the command line will recognise immediately. A surprisingly large proportion of “data analysis” is really getting the data into a form in which analysis becomes possible.
That is not glamorous work.
But it is essential work.
The book treats tidying as a first-class skill rather than an annoying chore to be hidden behind the statistics.
And that is precisely right.
Transformation is the heart of the work
Once data is tidy, the interesting work begins.
The book teaches the vocabulary for manipulating data: selecting variables, filtering observations, creating new variables, grouping records and calculating summaries.
These operations may look simple individually. Together, they form a remarkably expressive language for asking questions of data.
This is where I see an especially strong connection with the Unix toolbox.
A Textsmith is already accustomed to thinking in transformations:
input β filter β transform β aggregate β outputThe tools may be different, but the mental model is familiar.
With Unix tools, we might use:
grep
awk
sort
uniq
cut
sedWith R and the tidyverse, we encounter another vocabulary for expressing similar kinds of transformations.
The important lesson is not that one toolbox replaces the other.
It is that data analysis is fundamentally a transformation problem.
Once you begin thinking that way, the boundary between “text processing” and “data analysis” becomes much less rigid.
A CSV file is still text.
A table is still structured information.
The question is what transformation you need to apply to it.
Visualisation is not decoration
Another major strength of R for Data Science is its treatment of visualisation.
The book’s second part, Visualize, goes considerably further than the first edition. It covers the layered grammar of graphics, exploratory data analysis, and the transformation of exploratory plots into graphics intended for communication.
The important thing here is that visualisation is not presented merely as a way of making reports prettier.
It is a way of thinking.
You plot something because you want to see what is there.
Perhaps there is an unexpected cluster.
Perhaps one value is wildly different from the others.
Perhaps two variables appear to move together.
Perhaps your original question was the wrong question.
A good visualisation can reveal something that a table of numbers makes difficult to notice.
The book’s treatment of exploratory data analysis captures this nicely: you generate questions, then search for answers by repeatedly visualising, transforming and modelling the data.
This is an important distinction between exploration and presentation.
The chart you use while trying to understand your data does not necessarily need to be the chart you eventually show someone else.
First, discover.
Then, explain.
That is good analytical practice regardless of which programming language or tool you use.
The book gets serious about messy data
One reason I would recommend this book even to someone who has no intention of becoming an R programmer is the attention it gives to the awkward parts of working with data.
Strings.
Regular expressions.
Dates and times.
Factors.
Missing values.
Joins.
These are precisely the areas where real-world datasets stop behaving like textbook examples.
The second edition gives these subjects their own space in the Transform section rather than hiding everything inside one enormous data-transformation chapter.
This is a welcome decision.
Consider missing values alone.
A beginner may think that data analysis means calculating averages, drawing graphs and finding correlations. An experienced analyst knows that the difficult question may be:
What exactly does this missing value mean?
Is the information unknown?
Was it never collected?
Does zero mean the same thing as missing?
Was the field deliberately left blank?
Was the data imported incorrectly?
These questions have nothing to do with fancy algorithms.
They have everything to do with taking data seriously.
That is one of the deeper lessons of this book: good analysis begins long before the statistical analysis.
Importing data from the real world
The second edition also substantially expands the import side of the story.
The Import section covers spreadsheets, databases, Apache Arrow, hierarchical data and web scraping.
That is significant because “data” rarely means “a nice CSV sitting conveniently in your current directory.”
It might be in a spreadsheet.
It might be in SQLite or another database.
It might be JSON returned by an API.
It might be nested data that needs to be turned into a useful rectangular structure.
It might be sitting on a web page.
And, increasingly, it may be too large to treat as an ordinary in-memory data frame. The book introduces Arrow for working with out-of-memory data, while still deliberately keeping its primary focus on small datasets.
This is another lesson that transfers beautifully to the command line:
data analysis starts with getting the data.
You cannot analyse information you cannot access, parse or understand.
And then there is programming
For a book aimed at data science, R for Data Science does something I particularly appreciate: it does not treat programming as something that happens before data analysis and then disappears.
The second edition has a dedicated Program section covering functions, iteration and a field guide to base R.
The authors make a particularly good point here: code is not merely instructions for the computer. It is also communication between programmers.
That includes communication with your future self.
This is a deeply Unix-like idea.
A good shell script is not merely something that happens to work. It should be understandable. A good awk program should express its intent. A good pipeline should make its transformation visible.
The same principle applies to R.
Once your analysis grows beyond a few commands, programming discipline matters.
Write functions instead of copying the same code repeatedly.
Use iteration when you need to perform the same operation on many inputs.
Keep projects organised.
Use consistent style.
Learn to read code written by others.
These are programming lessons, but they are also textsmithing lessons.
An interesting omission: modelling
There is one important difference between this edition and what some readers may remember from the first edition.
The dedicated modelling section has gone.
This might initially sound surprising. After all, the title promises data science, and modelling is clearly part of data science.
But the authors explain that they never had enough space to do modelling justice. Rather than provide a superficial treatment, they removed the section and point readers towards Tidy Modeling with R and the tidymodels ecosystem.
I think this is a strength rather than a weakness.
A good technical book knows what not to cover.
There is a temptation in technical writing to make a book comprehensive by adding everything remotely related to the subject. The result is often a book that mentions everything and teaches nothing particularly well.
R for Data Science instead concentrates on the parts of data work it believes can be taught properly within its scope.
And that scope is already substantial.
Small data is not lesser data
There is another point worth emphasising for the audience of this blog.
This is not a book about enormous datasets.
The authors explicitly say that the book primarily focuses on small, in-memory datasets. The tools can handle hundreds of megabytes comfortably and, with care, several gigabytes, but the book’s philosophy is to start with small data.
That is entirely reasonable.
There is an unfortunate tendency to equate serious data analysis with enormous quantities of data.
But a five-thousand-row CSV can contain a difficult analytical problem.
A two-million-row dataset can contain a trivial one.
The intellectual challenge is not determined by the number of rows.
And learning how to clean, inspect, transform, visualise and communicate a small dataset is an excellent foundation for dealing with larger ones later.
In fact, the book makes essentially this argument: you need experience with small data before you can tackle big data effectively.
That fits our own exploration rather well.
The Textsmith does not need a Hadoop cluster to learn how to think about data.
A CSV file and a terminal are quite enough to teach many important lessons.
Quarto and reproducible analysis
There is one final aspect of the second edition that deserves particular attention from anyone interested in plaintext publishing.
The book itself was built with Quarto.
And its final section, Communicate, has been updated around Quarto rather than R Markdown.
This makes the book especially interesting from the Textsmith perspective.
The analysis does not have to end at the terminal.
You can transform data programmatically, generate visualisations, write your explanation alongside the code and produce a finished document from the same source.
That is reproducible publishing.
It is the same basic philosophy we have encountered throughout our exploration of plaintext tools:
keep the source, keep the transformation, and make the result reproducible.
The fact that the authors use Quarto to build the book itself makes the philosophy wonderfully concrete.
What I like most about this book
What I like most about R for Data Science is that it does not make data analysis look like a magical activity performed by statisticians with enormous computers.
It breaks the work down into understandable transformations.
Get the data.
Tidy it.
Transform it.
Look at it.
Ask questions.
Transform it again.
Visualise it.
Communicate what you have learned.
And surround the whole process with good programming practice.
That is a very practical definition of data science.
It also happens to be a very good definition of textsmithing.
The tools differ, but the habits are remarkably similar.
Who should read it?
I would recommend the second edition particularly strongly to:
- R programmers who want to become better data analysts.
- Developers who already know R but have never developed a systematic data-analysis workflow.
- Analysts who want a disciplined approach to cleaning and transforming data.
- People interested in the tidyverse and its way of expressing data transformations.
- Anyone who wants to understand exploratory data analysis rather than simply produce charts.
- Text-oriented programmers who want to see how another language approaches structured information.
I would not recommend it as the first book for someone who has never programmed in R.
That distinction is important.
The book teaches data science with R, not R from scratch.
The Textsmith’s verdict
There is something wonderfully appropriate about putting R for Data Science on the Textsmith’s Bookshelf during a month devoted to data analysis from the terminal.
At first glance, R might seem to belong to a completely different world from Unix text processing.
One has statisticians, data frames and ggplot2.
The other has pipes, awk, sed, sort and plain text.
But underneath the syntax lies a shared idea:
information becomes useful when we can transform it.
That is the real lesson I take from this book.
The goal is not to become a collector of R functions. It is not to memorise every tidyverse verb. It is not even necessarily to become a professional data scientist.
The goal is to develop the habit of looking at a dataset and asking:
What is its structure? What is wrong with that structure? What transformation would make the question easier to answer? What does the result tell me? And how can I communicate it clearly?
Those are Textsmith questions.
And that is why R for Data Science, 2nd Edition earns its place on the bookshelf.
The book is available to read freely online, and the authors explicitly intend the website to remain free.
Read R for Data Science, 2nd Edition
Book details
Title: R for Data Science, 2nd Edition Authors: Hadley Wickham, Mine Γetinkaya-Rundel, Garrett Grolemund Published: 2023 Focus: Data analysis with R and the tidyverse Level: Intermediate β assumes familiarity with R Format: Freely available online, with a print edition available Textsmith rating: β β β β β
Verdict: A superb guide to the craft of data analysis for programmers who already know R.