Skip to Content
R for Data Science, 2nd Edition
book

R for Data Science, 2nd Edition

by Hadley Wickham, Mine Çetinkaya-Rundel, Garrett Grolemund
June 2023
Beginner to intermediate
576 pages
12h 57m
English
O'Reilly Media, Inc.
Book available
Content preview from R for Data Science, 2nd Edition

Chapter 2. Workflow: Basics

You now have some experience running R code. We didn’t give you many details, but you’ve obviously figured out the basics or you would’ve thrown this book away in frustration! Frustration is natural when you start programming in R because it is such a stickler for punctuation, and even one character out of place can cause it to complain. But while you should expect to be a little frustrated, take comfort in that this experience is typical and temporary: it happens to everyone, and the only way to get over it is to keep trying.

Before we go any further, let’s ensure you’ve got a solid foundation in running R code and that you know some of the most helpful RStudio features.

Coding Basics

Let’s review some basics we’ve omitted so far in the interest of getting you plotting as quickly as possible. You can use R to do basic math calculations:

1 / 200 * 30
#> [1] 0.15
(59 + 73 + 2) / 3
#> [1] 44.66667
sin(pi / 2)
#> [1] 1

You can create new objects with the assignment operator <-:

x <- 3 * 4

Note that the value of x is not printed, it’s just stored. If you want to view the value, type x in the console.

You can combine multiple elements into a vector with c():

primes <- c(2, 3, 5, 7, 11, 13)

And basic arithmetic on vectors is applied to every element of the vector:

primes * 2
#> [1]  4  6 10 14 22 26
primes - 1
#> [1]  1  2  4  6 10 12

All R statements where you create objects, assignment statements, have the same form:

object_name <- value

When ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

R Programming for Statistics and Data Science

R Programming for Statistics and Data Science

365 Careers Ltd.
R for Data Science

R for Data Science

Hadley Wickham, Garrett Grolemund

Publisher Resources

ISBN: 9781492097396Errata PageSupplemental Content