hadley / r4ds

R for data science: a book
http://r4ds.hadley.nz
Other
4.51k stars 4.19k forks source link

code does not work #1592

Closed di-olcay closed 10 months ago

di-olcay commented 10 months ago

the code in section 13.4.3 do not work when copied to clipboard and pasted directly.

mine-cetinkaya-rundel commented 10 months ago

Both chunks work for me, reprex below. Perhaps you haven't loaded the packages?

library(tidyverse)
library(nycflights13)

flights |> 
  filter(arr_delay > 0) |> 
  group_by(year, month, day) |> 
  summarize(
    behind = mean(arr_delay),
    n = n(),
    .groups = "drop"
  )
#> # A tibble: 365 × 5
#>     year month   day behind     n
#>    <int> <int> <int>  <dbl> <int>
#>  1  2013     1     1   32.5   461
#>  2  2013     1     2   32.0   535
#>  3  2013     1     3   27.7   460
#>  4  2013     1     4   28.3   297
#>  5  2013     1     5   22.6   238
#>  6  2013     1     6   24.4   381
#>  7  2013     1     7   27.8   243
#>  8  2013     1     8   20.8   275
#>  9  2013     1     9   25.6   287
#> 10  2013     1    10   27.3   220
#> # ℹ 355 more rows

flights |> 
  group_by(year, month, day) |> 
  summarize(
    behind = mean(arr_delay[arr_delay > 0], na.rm = TRUE),
    ahead = mean(arr_delay[arr_delay < 0], na.rm = TRUE),
    n = n(),
    .groups = "drop"
  )
#> # A tibble: 365 × 6
#>     year month   day behind ahead     n
#>    <int> <int> <int>  <dbl> <dbl> <int>
#>  1  2013     1     1   32.5 -12.5   842
#>  2  2013     1     2   32.0 -14.3   943
#>  3  2013     1     3   27.7 -18.2   914
#>  4  2013     1     4   28.3 -17.0   915
#>  5  2013     1     5   22.6 -14.0   720
#>  6  2013     1     6   24.4 -13.6   832
#>  7  2013     1     7   27.8 -17.0   933
#>  8  2013     1     8   20.8 -14.3   899
#>  9  2013     1     9   25.6 -13.0   902
#> 10  2013     1    10   27.3 -16.4   932
#> # ℹ 355 more rows

Created on 2023-11-09 with reprex v2.0.2

di-olcay commented 10 months ago

Thanks for the reply. Find out that the error was caused by a package conflict and solved it with your tip.