Data science is an exciting discipline that allows you to turn raw data into understanding, insight, and knowledge.
The goal of “R for Data Science” is to help you learn the most important tools in R that will allow you to do data science.
After reading this book, you’ll have the tools to tackle a wide variety of data science challenges, using the best parts of R
Data science is a huge field, and there’s no way you can master it by reading a single book. The goal of this book is to give you a solid foundation in the most important tools.
Our model of the tools needed in a typical data science project looks something like this:
1 Importing Data
First you must import your data into R. This typically means that you take data stored in a file, database, or web application programming interface (API), and load it into a data frame in R. If you can’t get your data into R, you can’t do data science on it!
2 Tidying Data
Once you’ve imported your data, it is a good idea to tidy it. Tidying your data means storing it in a consistent form that matches the semantics of the dataset with the way it is stored.
In brief, when your data is tidy, each column is a variable, and each row is an observation.
Tidy data is important because the consistent structure lets you focus your struggle on questions about the data, not fighting to get the data into the right form for different functions.
3 Transforming data
Once you have tidy data, a common first step is to transform it. Transformation includes narrowing in on observations of interest (like all people in one city, or all data from the last year), creating new variables that are functions of existing variables (like computing speed from distance and time), and calculating a set of summary statistics (like counts or means).
Together, tidying and transforming are called wrangling, because getting your data in a form that’s natural to work with often feels like a fight!
4.1 Visualisation
It is is a fundamentally human activity. A good visualisation will show you things that you did not expect, or raise new questions about the data.
A good visualisation might also hint that you’re asking the wrong question, or you need to collect different data. Visualisations can surprise you, but don’t scale particularly well because they require a human to interpret them.
4.2 Modelling
Models are a fundamentally mathematical or computational tool, so they generally scale well. Even when they don’t, it’s usually cheaper to buy more computers than it is to buy more brains!
But every model makes assumptions, and by its very nature a model cannot question its own assumptions. That means a model cannot fundamentally surprise you.
5 Communication
The last step of data science is communication, an absolutely critical part of any data analysis project.
It doesn’t matter how well your models and visualisation have led you to understand the data unless you can also communicate your results to others.
Data exploration is the art of looking at your data, rapidly generating hypotheses, quickly testing them, then repeating again and again and again.
The goal of data exploration is to generate many promising leads that you can later explore in more depth.
Visualisation is a great place to start with R programming, because the payoff is so clear: you get to make elegant and informative plots that help you understand data. In data visualisation you’ll dive into visualisation, learning the basic structure of a ggplot2 plot, and powerful techniques for turning data into plots.
Visualisation alone is typically not enough, so in data transformation you’ll learn the key verbs that allow you to select important variables, filter out key observations, create new variables, and compute summaries.
Finally, in exploratory data analysis, you’ll combine visualisation and transformation with your curiosity and scepticism to ask and answer interesting questions about data.
Let’s review some basics we’ve so far omitted in the interests of getting you plotting as quickly as possible. You can use R as a calculator:
1 / 200 * 30 #> [1] 0.15 (59 + 73 + 2) / 3 #> [1] 44.66667 sin(pi / 2) #> [1] 1
You can create new objects with <-:
x <- 3 * 4
All R statements where you create objects, assignment statements, have the same form:
object_name <- value
When reading that code say “object name gets value” in your head.
You will make lots of assignments and <- is a pain to type. Don’t be lazy and use =: it will work, but it will cause confusion later. Instead, use RStudio’s keyboard shortcut: Alt + - (the minus sign). Notice that RStudio automagically surrounds <- with spaces, which is a good code formatting practice. Code is miserable to read on a good day, so giveyoureyesabreak and use spaces.
Object names must start with a letter, and can only contain letters, numbers, _ and .. You want your object names to be descriptive, so you’ll need a convention for multiple words. We recommend snake_case where you separate lowercase words with _.
i_use_snake_case otherPeopleUseCamelCase some.people.use.periods And_aFew.People_RENOUNCEconvention
We’ll come back to code style later, in functions.
You can inspect an object by typing its name:
x #> [1] 12
Make another assignment:
this_is_a_really_long_name <- 2.5
has a large collection of built-in functions that are called like this:
function_name(arg1 = val1, arg2 = val2, …)
Let’s try using seq() which makes regular sequences of numbers and, while we’re at it, learn more helpful features of RStudio. Type se and hit TAB. A popup shows you possible completions. Specify seq() by typing more (a “q”) to disambiguate, or by using ↑/↓ arrows to select. Notice the floating tooltip that pops up, reminding you of the function’s arguments and purpose. If you want more help, press F1 to get all the details in the help tab in the lower right pane.
Press TAB once more when you’ve selected the function you want. RStudio will add matching opening (() and closing ()) parentheses for you. Type the arguments 1, 10 and hit return.
seq(1, 10) #> [1] 1 2 3 4 5 6 7 8 9 10
Type this code and notice you get similar assistance with the paired quotation marks:
x <- “hello world”
Quotation marks and parentheses must always come in a pair. RStudio does its best to help you, but it’s still possible to mess up and end up with a mismatch. If this happens, R will show you the continuation character “+”:
x <- “hello +
The + tells you that R is waiting for more input; it doesn’t think you’re done yet. Usually that means you’ve forgotten either a ” or a ). Either add the missing pair, or press ESCAPE to abort the expression and try again.
If you make an assignment, you don’t get to see the value. You’re then tempted to immediately double-check the result:
y <- seq(1, 10, length.out = 5) y #> [1] 1.00 3.25 5.50 7.75 10.00
This common action can be shortened by surrounding the assignment with parentheses, which causes assignment and “print to screen” to happen.
(y <- seq(1, 10, length.out = 5)) #> [1] 1.00 3.25 5.50 7.75 10.00