Please briefly describe your problem and what output you expect. If you have a question, please don't use this form. Instead, ask on https://stackoverflow.com/ or https://community.rstudio.com/.
Please include a minimal reproducible example (AKA a reprex). If you've never heard of a reprex before, start by reading https://www.tidyverse.org/help/#reprex.
Brief description of the problem
Multiple exercises use concepts, such as pipes, that are yet to be covered in the book. Here is a list of solutions with this issue.
Also, one of the below code examples does not produce the correct (at least as I think it should be, I could be wrong) answer due to mathematical error
5.5.3 {unnumbered exercise} - uses pipes which isn't introduced until 5.6
arrange(flights, air_time) %>%
select(origin, dest, air_time) %>%
head()
Suggested replacement code-
new <- arrange(flights, air_time)
select(new, origin, dest, air_time)
head(new)
Exercise 5.4.1 {.unnumbered .exercise} - uses regular expressions which isn't introduced until 5.6, I only know this because I asked a friend who is much more experienced than I in R. I do not know how to fix this code since I have just started chapter 9! I wil
Exercise 5.5.1 - Uses pipes and code does not compute the correct values and I find the use of %% confusing and seems to add more code.
mutate(flights, dep_time_mins = dep_time%/%100 * 60 + dep_time%%100,
sched_dep_time_mins = sched_dep_time%/%100 * 60 + sched_dep_time%%100) %>%
select(dep_time, dep_time_mins, sched_dep_time, sched_dep_time_mins)
This is the resulting tibble
A tibble: 336,776 x 4
dep_time dep_time_mins sched_dep_time sched_dep_time_mins
1 517 317 515 315
2 533 333 529 329
3 542 342 540 340
dep_time_mins should be 310 not 317
(517/100)*60
[1] 310.2
and sched_dep_time_mins should be 309 not 315
(515/100)*60
[1] 309
I suggest this code which does not use pipes and skips the %% notation.
new2<- mutate(flights,
dep_time_mins = (dep_time / 100)*60,
sched_dep_time_mins = (sched_dep_time / 100)*60)
select(new2, dep_time, dep_time_mins, sched_dep_time, sched_dep_time_mins)
Which results in this tibble with the correct values
A tibble: 336,776 x 4
dep_time dep_time_mins sched_dep_time sched_dep_time_mins
1 517 310. 515 309
2 533 320. 529 317.
Here is the code in the portion which creates a function first
time2mins <- function(x) {
(x / 100) * 60
}
new3 <- mutate(flights,
dep_time_mins = time2mins(dep_time),
sched_dep_time_mins = time2mins(sched_dep_time))
select(new3,dep_time, dep_time_mins, sched_dep_time, sched_dep_time_mins)
Exercises 5.5.2 & 5.5.3 - solution doesn't seem to work and uses pipes
I do not understand how this code results in something that shows the problem is due to a time zone or next day departure. Maybe the solutions are switched around because I see the 1440 is a 25 hour period so that would account for departing the next day? To me if it was due to a time zone difference the differences between air time and `arr_time - dep_time' would always be in 60 min increments, which the results are not. More clarification is needed, at least for me!
The comment in 5.5.2 "As with the previous question, we will need to Since arr_time and dep_time may be in different time zones," suggests these 2 solutions are inverted. I can't quite reconcile this in my mind how to fix it as I am muddled going back and forth!
Exercise 5.5.6 uses Tibble in solution which has not been introduced. I learn from this but would not have used it on my own.