Errata

Hands-On Programming with R

Errata for Hands-On Programming with R

Submit your own errata for this product.

The errata list is a list of errors and their corrections that were found after the product was released. If the error was corrected in a later version or reprint the date of the correction will be displayed in the column titled "Date Corrected".

The following errata were submitted by our customers and approved as valid errors by the author or editor.

Color key: Serious technical mistake Minor technical mistake Language or formatting error Typo Question Note Update

Version Location Description Submitted By Date submitted Date corrected
Printed
Page x
2nd para

Duplicate 'any' in the sentence:

"I would not recommend that any any ..."

William Byrd  Sep 02, 2014  Jan 23, 2015
Page x, xi
x, 2nd paragraph; xi 4th paragraph in box

"judgement" is spelled judgment, at least in American English, though judgement is common in British English. I'll let you make the call.

Note from the Author or Editor:
Please replace judgement with judgment throughout

guyrichard  May 04, 2015  Mar 14, 2025
Other Digital Version
loc 1708
First paragraph

The page reads (location 1708 of 7221, Kindle edition, iPad)
Every CSV file shares this basic format:
"face","suit","value"
"king","spades",13
"queen","spades,12
"jack","spades,11
"ten","spades,10
"nine","spades,9
... and so on.

With the missing quotes in place, It should read
Every CSV file shares this basic format:
"face","suit","value"
"king","spades",13
"queen","spades",12
"jack","spades",11
"ten","spades",10
"nine","spades",9
... and so on.

Note from the Author or Editor:
The missing quotes should be added. There appears to be a quote missing at the end of each suit name.

Cilio Ziviani  Dec 12, 2015  Mar 14, 2025
Other Digital Version
loc2811
First paragraph, location 2811, Kindle edition

The book reads
This arrangement creates a quandry for R whenever R runs a function.

It should read
This arrangement creates a quandary for R whenever R runs a function.

Note from the Author or Editor:
I think the reviewer has spotted an html tag that slipped through to the text. The html tag appears in my email notification ( a <p> tag at the end of the sentence), but the html tag does not appear above in the O'Reilly Errata portal. The tag should be removed.

Cilio Ziviani  Dec 16, 2015  Mar 14, 2025
Page Chapter 2
In "LIBRARY" section, after figure 2-1

When describing the first example use of qplot(), there is a sentence which states:

"Together, the vectors x and y describe a set of 10 points."

This sentence should instead read:

"Together, the vectors x and y describe a set of 11 points."

Note from the Author or Editor:
Please change

"Together, the vectors x and y describe a set of 10 points."

to

"Together, the vectors x and y describe a set of 11 points."

Anonymous  Oct 07, 2019  Mar 14, 2025
Page Chapter 3
Note 1 near end of "Loading Data" section

The note contains a sentence beginning with:

"To see a different number of rows, give head or tails a second argument..."

"tails" should instead be "tail".

Note from the Author or Editor:
Please change

"To see a different number of rows, give head or tails a second argument"

to

"To see a different number of rows, give head or tail a second argument"

Anonymous  Oct 08, 2019  Mar 14, 2025
Page Chapter 8
Second paragraph of "Attributes" section

The paragraph reads:

"You can see an object’s attributes with attribute. If you run attribute on the DECK data frame that you created in Part II, you will see:"

The references to the "attribute" function should instead be "attributes". The paragraph should instead read:

"You can see an object’s attributes with attributes. If you run attributes on the DECK data frame..."

Note from the Author or Editor:
Please change

"You can see an object’s attributes with attribute. If you run attribute on the DECK data frame that you created in Part II, you will see:"

to

"You can see an object’s attributes with attributes. If you run attributes on the DECK data frame that you created in Part II, you will see:"

Anonymous  Oct 09, 2019  Mar 14, 2025
Page Appendix E
Second paragraph of "trace" section

Third sentence reads: "R will prints trace:<the function>..."

"prints" should be "print".

Note from the Author or Editor:
Please change prints to print.

Anonymous  Oct 16, 2019  Mar 14, 2025
Page Environments
before figure 6.5

Environment is misspelled... see copy/paste below ...

Then you call the function. R responds by setting up a new runtime environment. This environment will be a child of the function’s origin enviornment. R will copy each of the function’s arguments into the runtime environment and then make the runtime environment the new active environment.

Note from the Author or Editor:
Please change "enviornment" to "environment"

Lonnie Sanders III  Apr 09, 2022  Mar 14, 2025
Printed
Page 1
Pg 94 (3rd para) and 95 (1st para)

Parenvs function is found in pryr package, not devtools

Dennis Yap San Hong  Oct 04, 2014  Jan 23, 2015
Page 1 Chapter
Figure 1.4

Hi,

I think the figure 1.4 content should read [1] 1 4 3 8 5 12 instead of 1 4 4 6 6 8

Bye
Fabio

Note from the Author or Editor:
Figure 1-4 should be replaced with an image I can provide if you contact me at grolemund@gmail.com.

Anonymous  Nov 02, 2019  Mar 14, 2025
Printed
Page 2
9th line from top

tiny typos;
... seem frivilous, think of ...
->
... seem frivolous, think of ...


Yukitoshi Fujimura  Aug 21, 2014  Jan 23, 2015
Printed, PDF
Page 5
second paragraph

"The colon operator (+) returns�" should be "The colon operator (:) returns�"

Brian Drye  Jul 30, 2014  Jan 23, 2015
Page 5
2nd para

> 100:130
[1] 100 101 102 103 104 105 106 107 108 109 110 111 112
[14] 113 114 115 116 117 118 119 120 121 122 123 124 125
[25] 126 127 128 129 130

the above line [25] should be [27].

Note from the Author or Editor:
This is correct.

> 100:130
[1] 100 101 102 103 104 105 106 107 108 109 110 111 112
[14] 113 114 115 116 117 118 119 120 121 122 123 124 125
[25] 126 127 128 129 130

Should be changed to ([25] -> [27])

> 100:130
[1] 100 101 102 103 104 105 106 107 108 109 110 111 112
[14] 113 114 115 116 117 118 119 120 121 122 123 124 125
[27] 126 127 128 129 130

sreenivas vaddi  May 09, 2023  Mar 14, 2025
Printed
Page 11
Figure 1-4

The operation shown in t he figure is multiplication while the discussion preceding it is addition. The results shown is incorrect for multiplication (but correct for addition).

Note from the Author or Editor:
In Figure 1-4, each of the multiplication symbols (*) should be changed to an addition symbol (+). There are 10 total that should be corrected.

Dan Watts  Aug 07, 2014  Jan 23, 2015
Printed, PDF
Page 11
4th line bottom

Just need [,] notation for both column and row;

die%*%die
## 91

should be

die%*%die
## [,1]
##[1,] 91

Note from the Author or Editor:
Yes, the code at the bottom of page 11 should read:

die %*% die
## [,1]
## [1,] 91

Yukitoshi Fujimura  Aug 21, 2014  Jan 23, 2015
Page 11
Figure 1-4

1 + 1 = 1 should be 1 + 1 = 2

Note from the Author or Editor:
Figure 1-4 should be replaced with an image I can provide if you contact me at grolemund@gmail.com.

Lars Koewing  Feb 21, 2020  Mar 14, 2025
Printed
Page 19
Second paragraph from bottom

In the second sentence of the second paragraph from the bottom, the word "function" (written in "code" font) is split between two lines (func... tion). Is it possible to fit this word on one line?

Garrett Grolemund
Garrett Grolemund
 
Dec 11, 2014  Jan 23, 2015
Page 22
Paragraph 4

'R packages, which are collections of functions WRITTED by R’s talented community of developers'

should be replaced with

'R packages, which are collections of functions WRITTEN by R’s talented community of developers'

Note from the Author or Editor:
Please replace writted with written

Nicola Gnecco  Jan 28, 2017  Mar 14, 2025
Printed
Page 27
3rd paragraph

"Numbers covered by a tall bar are no more common than ...." should be "Numbers covered by a tall bar are more common than ...."

Dan Watts  Aug 07, 2014  Jan 23, 2015
Printed
Page 39
After the second paragraph of text

The book reads:

sum(num)
## 6

It SHOULD say:

sum(int)
## 6

Garrett Grolemund
Garrett Grolemund
 
Dec 11, 2014  Jan 23, 2015
Printed
Page 40
3rd paragraph

"Integers are defined more precisely in your computer's memory than doubles (unless the integer is 'very' large or small)."
I understand what is written for large integers, but small (or very small) integers are close to zero. Perhaps it should have been "'very' large positive or negative".

Note from the Author or Editor:
Change text to read:

"Integers are defined more precisely in your computer's memory than doubles (unless the integer is very large)."

Dan Watts  Aug 07, 2014  Jan 23, 2015
Printed
Page 44
Code at bottom of the page with Spanish numbers from 1-6

"quatro" should be spelled "cuatro." It is listed twice in the code.

Garrett Grolemund
Garrett Grolemund
 
Dec 11, 2014  Jan 23, 2015
Page 45
1st line after 2nd example

"or a 1 x 2 x 3 hypercube". Hypercube is probably not the best word for this matrix, since being 3D, the "hyper-" isn't needed, and the sides aren't equal, so not a cube.

Note from the Author or Editor:
Please change "hypercube" to "array"

Matthew  Feb 19, 2015  Mar 14, 2025
Page 46
1st and 2nd line of "arrays" section

"array" breaks across lines without hyphen

Note from the Author or Editor:
Please fix

Matthew  Feb 19, 2015  Mar 14, 2025
Page 54
2nd paragraph

Typo: "[Lists] .. are used to create many more spohisticated types of R objects." should read "... sophisticated".

Note from the Author or Editor:
Please change spohisticated to sophisticated

Heiko Korndorf  Jan 03, 2018  Mar 14, 2025
Page 56
middle

When typed in the command str(df), this is what I get:

> str(df)
'data.frame': 3 obs. of 3 variables:
$ face : chr "ace" "two" "six"
$ suit : chr "clubs" "clubs" "clubs"
$ value: num 1 2 3

Unlike what's in the book, I don't see the outputs with "Factor w/ 3 levels" but "chr" instead after $ face. What did I do wrong?

Note from the Author or Editor:
The behavior of R has changed since I described it here. We should replace this section from page 56:
---
str(df)
## 'data.frame': 3 obs. of 3 variables:
## $ face : Factor w/ 3 levels "ace","six","two": 1 3 2
## $ suit : Factor w/ 1 level "clubs": 1 1 1
## $ value: num 1 2 3

Notice that R saved your character strings as factors. I told you that R likes factors! It is not a very big deal here, but you can prevent this behavior by adding the argument stringsAsFactors = FALSE to data.frame:

df <- data.frame(face = c("ace", "two", "six"),
suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3),
stringsAsFactors = FALSE)
---

With this much shorter section:

---
str(df)
## 'data.frame': 3 obs. of 3 variables:
## $ face : chr "ace" "two" "six"
## $ suit : chr "clubs" "clubs" "clubs"
## $ value: num 1 2 3
---

Jin Zhang  Jan 12, 2022  Mar 14, 2025
Printed
Page 82
Last paragraph ("Next, you can use...")

On the second line of that paragraph, "individually" is misspelled. (It has an extra u.)

Garrett Grolemund
Garrett Grolemund
 
Dec 11, 2014  Jan 23, 2015
Page 94
2nd paragraph and code underneath

The parenvs() function has been removed from devtools, because is now in the pryr package. Consequently, devtools should be replaced with pryr.

Note from the Author or Editor:
Every reference to devtools in Chapter 8 Environments should be swapped with pryr. This can be done with a find and replace.

Adrian Danescu  Aug 12, 2020  Mar 14, 2025
Printed
Page 96
2nd and 3rd paragraphs

2nd paragraph: ..."the parenvs function in the devtools package.''
3rd paragraph (code example: "library(devtools)"

The 'parenvs' function is no longer in the 'devtools package; it has been move to the "pryr" package.

Bill Kehoe  Dec 25, 2014  Jan 23, 2015
Printed
Page 105
First paragraph, second line

"Pristine" is misspelled. (It has an extra i.)

Garrett Grolemund
Garrett Grolemund
 
Dec 11, 2014  Jan 23, 2015
PDF
Page 118
"If a subtask seems complicated, try to divide it again into even subtasks that are even more simple"

"If a subtask seems complicated, try to divide it again into even subtasks that are even more simple"

should be

"If a subtask seems complicated, try to divide it again into smaller subtasks that are even more simple"

Note from the Author or Editor:
Change "If a subtask seems complicated, try to divide it again into even subtasks that are even more simple" to "If a subtask seems complicated, try to divide it again into smaller subtasks that are even more simple"

Eric Watt  Dec 13, 2014  Jan 23, 2015
Printed
Page 125
Numbered list at the top of the page

Numbers 2 and 3 are out of sequence in the list.

1) Test whether the symbols are three of a kind.

2) Look up the prize for three of a kind based on the common symbol.

3) Test whether the symbols are all bars.

Garrett Grolemund
Garrett Grolemund
 
Dec 11, 2014  Jan 23, 2015
PDF
Page 127
Middle of page

Original wrong code: length(unique(symbols) == 1)

Correct code: length(unique(symbols)) == 1

Dennis Yap San Hong  Oct 07, 2014  Jan 23, 2015
Printed
Page 134
1st paragraph

"And when cherries equals three ..." should be "And when cherries equal two ..."

Dan Watts  Aug 10, 2014  Jan 23, 2015
Printed
Page 137
Code just above "Summary"

The last example of the play function is missing a double B. It should read:

play()
## "BB" "BB" "BB"
## 25

Garrett Grolemund
Garrett Grolemund
 
Dec 11, 2014  Jan 23, 2015
Printed
Page 163
Second paragraph from bottom

The second to last paragraph ("On the first run, the for loop...") should end with a period and not a colon.

Garrett Grolemund
Garrett Grolemund
 
Dec 11, 2014  Jan 23, 2015
PDF
Page 171
Last paragraph and code below

function used is called abs_set in the text, but abs_sets in the code

Note from the Author or Editor:
The code on page 171 and 172 should be altered. Specifically the line on page 171: "abs_sets <- function(vec){" should be changed to "abs_set <- function(vec){", and the line on page 172: "system.time(abs_sets(long))" should be changed to "system.time(abs_set(long))"

Eric Watt  Dec 13, 2014  Jan 23, 2015
PDF
Page 178
Code examples

Code does not run as written. Example given is
system.time(
output <- rep(NA, 1000000)
for (i in 1:1000000) {
output[i] <- i + 1
}
)

Had to be modified to

system.time({
output <- rep(NA, 1000000)
for (i in 1:1000000) {
output[i] <- i + 1
}
})

Note from the Author or Editor:
Add a set of braces immediately inside the parentheses that follow system.time (like this):

system.time({
output <- rep(NA, 1000000)
for (i in 1:1000000) {
output[i] <- i + 1
}
})

Eric Watt  Dec 13, 2014  Jan 23, 2015
Printed, PDF
Page 180
last paragraph, first sentence

typo: "(matix-ized?)" should be "(matrix-ized?)"

Brian Drye  Aug 07, 2014  Jan 23, 2015
PDF
Page 180
1st paragraph

'In fact, the simulation takes 342,308 seconds to run, which is about 5.7 minutes'

Should be 342.308 seconds.

Note from the Author or Editor:
342,308 needs to be changed to 342.308

Eric Watt  Dec 13, 2014  Jan 23, 2015
Page 180
Code example for get_many_symbols

The code mentioned is:

get_many_symbols <- function(n) {
wheel <- c("DD", "7", "BBB", "BB", "B", "C", "0")
vec <- sample(wheel, size = 3 * n, replace = TRUE,
prob = c(0.03, 0.03, 0.06, 0.1, 0.25, 0.01, 0.52))
matrix(vec, ncol = 3)
}

When in fact it should be:

get_many_symbols <- function(n) {
wheel <- c("DD", "7", "BBB", "BB", "B", "C", "0")
vec <- sample(wheel, size = 3 * n, replace = TRUE,
prob = c(0.03, 0.03, 0.06, 0.1, 0.25, 0.01, 0.52))
matrix(vec, ncol = 3, byrow = TRUE)
}

Note from the Author or Editor:
Agreed. Please replace:

get_many_symbols <- function(n) {
wheel <- c("DD", "7", "BBB", "BB", "B", "C", "0")
vec <- sample(wheel, size = 3 * n, replace = TRUE,
prob = c(0.03, 0.03, 0.06, 0.1, 0.25, 0.01, 0.52))
matrix(vec, ncol = 3)
}

with (includes a byrow argument):

get_many_symbols <- function(n) {
wheel <- c("DD", "7", "BBB", "BB", "B", "C", "0")
vec <- sample(wheel, size = 3 * n, replace = TRUE,
prob = c(0.03, 0.03, 0.06, 0.1, 0.25, 0.01, 0.52))
matrix(vec, ncol = 3, byrow = TRUE)
}

Jash Shah  Sep 12, 2016  Mar 14, 2025
Page 180
2nd line

342,308 seconds .. 5.7 minutes (inconsistent use of comma as decimal point).

Note from the Author or Editor:
Please replace "342,308" seconds with "about 342 seconds"

Anonymous  Nov 23, 2016  Mar 14, 2025
Printed
Page 192
Third paragraph

In the sentence, "If this is your first time installing a package...", delete the extraneous "of."

It should say: "... choose an online mirror to install from."

Garrett Grolemund
Garrett Grolemund
 
Dec 11, 2014  Jan 23, 2015
Page 203
2nd paragraph

Update

With R 4.0.0, character strings are no longer converted to factors by default with read.table(). Paragraph is now inaccurate.

Note from the Author or Editor:
R has changed its behavior since I wrote this. We can and should cut this whole section from the book:
---
D.3.1.5 stringsAsFactors
R reads in numbers just as you’d expect, but when R comes across character strings (e.g., letters and words) it begins to act strangely. R wants to convert every character string into a factor. This is R’s default behavior, but I think it is a mistake. Sometimes factors are useful. At other times, they’re clearly the wrong data type for the job. Also factors cause weird behavior, especially when you want to display data. This behavior can be surprising if you didn’t realize that R converted your data to factors. In general, you’ll have a smoother R experience if you don’t let R make factors until you ask for them. Thankfully, it is easy to do this.

Setting the argument stringsAsFactors to FALSE will ensure that R saves any character strings in your data set as character strings, not factors. To use stringsAsFactors, you’d write:

read.table("poker.csv", sep = ",", header = TRUE, stringsAsFactors = FALSE)
If you will be loading more than one data file, you can change the default factoring behavior at the global level with:

options(stringsAsFactors = FALSE)
This will ensure that all strings will be read as strings, not as factors, until you end your R session, or rechange the global default by running:

options(stringsAsFactors = TRUE)
---

Sarah Dowding  Dec 03, 2020  Mar 14, 2025