hadley / r4ds

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

mutate() function not accepting .before=/.after= arguments #1549

Closed lucusmueller closed 12 months ago

lucusmueller commented 1 year ago

Trying to reproduce examples from the book 'R for Data Science' (2nd ed), chapter 'Data Transformation', section headed 'mutate()'

# Windows 7 pro 64bit, Service Pack 1
# RStudio version 1.3.1093
# R.Version()   -> # 4.3.1

library(nycflights13)
library(tidyverse)
nycflights13::flights
glimpse(flights)
flights |> 
  mutate(
    gain = dep_delay - arr_delay,
    speed = distance / air_time * 60
  )
flights |> 
  mutate(
    gain = dep_delay - arr_delay,
    speed = distance / air_time * 60,
    .before = 1
  )
flights |> 
  mutate(
    gain = dep_delay - arr_delay,
    speed = distance / air_time * 60,
    .after = day
  )

The two new columns gain & speed are invariably added to the right end, despite the arguments .before=1 and .after=day in the latter two calls, contrary to the examples presented in the book. Rather, the arguments are interpreted as new variables to be created, named .before resp. .after, and coded 1 invariably. By contrast, the arguments .before/.after worked well when I used them in relocate() functions, as suggested by their use of "tidy selection, as opposed to "data masking" used by mutate() [see https://dplyr.tidyverse.org/articles/programming.html, which I might have to read five more times in order to perhaps know what to do :) ]. => Why does my R not reproduce the results presented in the book, despite using the identical code?

mine-cetinkaya-rundel commented 12 months ago

You're possibly using a different version of dplyr? See reprex below that also includes session info:

library(tidyverse)
library(nycflights13)

flights |> 
  mutate(
    gain = dep_delay - arr_delay,
    speed = distance / air_time * 60
  )
#> # A tibble: 336,776 × 21
#>     year month   day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#>    <int> <int> <int>    <int>          <int>     <dbl>    <int>          <int>
#>  1  2013     1     1      517            515         2      830            819
#>  2  2013     1     1      533            529         4      850            830
#>  3  2013     1     1      542            540         2      923            850
#>  4  2013     1     1      544            545        -1     1004           1022
#>  5  2013     1     1      554            600        -6      812            837
#>  6  2013     1     1      554            558        -4      740            728
#>  7  2013     1     1      555            600        -5      913            854
#>  8  2013     1     1      557            600        -3      709            723
#>  9  2013     1     1      557            600        -3      838            846
#> 10  2013     1     1      558            600        -2      753            745
#> # ℹ 336,766 more rows
#> # ℹ 13 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
#> #   tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
#> #   hour <dbl>, minute <dbl>, time_hour <dttm>, gain <dbl>, speed <dbl>

flights |> 
  mutate(
    gain = dep_delay - arr_delay,
    speed = distance / air_time * 60,
    .before = 1
  )
#> # A tibble: 336,776 × 21
#>     gain speed  year month   day dep_time sched_dep_time dep_delay arr_time
#>    <dbl> <dbl> <int> <int> <int>    <int>          <int>     <dbl>    <int>
#>  1    -9  370.  2013     1     1      517            515         2      830
#>  2   -16  374.  2013     1     1      533            529         4      850
#>  3   -31  408.  2013     1     1      542            540         2      923
#>  4    17  517.  2013     1     1      544            545        -1     1004
#>  5    19  394.  2013     1     1      554            600        -6      812
#>  6   -16  288.  2013     1     1      554            558        -4      740
#>  7   -24  404.  2013     1     1      555            600        -5      913
#>  8    11  259.  2013     1     1      557            600        -3      709
#>  9     5  405.  2013     1     1      557            600        -3      838
#> 10   -10  319.  2013     1     1      558            600        -2      753
#> # ℹ 336,766 more rows
#> # ℹ 12 more variables: sched_arr_time <int>, arr_delay <dbl>, carrier <chr>,
#> #   flight <int>, tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>,
#> #   distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>

flights |> 
  mutate(
    gain = dep_delay - arr_delay,
    speed = distance / air_time * 60,
    .after = day
  )
#> # A tibble: 336,776 × 21
#>     year month   day  gain speed dep_time sched_dep_time dep_delay arr_time
#>    <int> <int> <int> <dbl> <dbl>    <int>          <int>     <dbl>    <int>
#>  1  2013     1     1    -9  370.      517            515         2      830
#>  2  2013     1     1   -16  374.      533            529         4      850
#>  3  2013     1     1   -31  408.      542            540         2      923
#>  4  2013     1     1    17  517.      544            545        -1     1004
#>  5  2013     1     1    19  394.      554            600        -6      812
#>  6  2013     1     1   -16  288.      554            558        -4      740
#>  7  2013     1     1   -24  404.      555            600        -5      913
#>  8  2013     1     1    11  259.      557            600        -3      709
#>  9  2013     1     1     5  405.      557            600        -3      838
#> 10  2013     1     1   -10  319.      558            600        -2      753
#> # ℹ 336,766 more rows
#> # ℹ 12 more variables: sched_arr_time <int>, arr_delay <dbl>, carrier <chr>,
#> #   flight <int>, tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>,
#> #   distance <dbl>, hour <dbl>, minute <dbl>, time_hour <dttm>

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

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.3.1 (2023-06-16) #> os macOS Sonoma 14.1 #> system aarch64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/New_York #> date 2023-11-09 #> pandoc 3.1.8 @ /opt/homebrew/bin/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> cli 3.6.1 2023-03-23 [1] CRAN (R 4.3.0) #> colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.3.0) #> digest 0.6.33 2023-07-07 [1] CRAN (R 4.3.0) #> dplyr * 1.1.3 2023-09-03 [1] CRAN (R 4.3.0) #> evaluate 0.22 2023-09-29 [1] CRAN (R 4.3.1) #> fansi 1.0.5 2023-10-08 [1] CRAN (R 4.3.1) #> fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.0) #> forcats * 1.0.0 2023-01-29 [1] CRAN (R 4.3.0) #> fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.0) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.0) #> ggplot2 * 3.4.4 2023-10-12 [1] CRAN (R 4.3.1) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.3.0) #> gtable 0.3.4 2023-08-21 [1] CRAN (R 4.3.0) #> hms 1.1.3 2023-03-21 [1] CRAN (R 4.3.0) #> htmltools 0.5.6.1 2023-10-06 [1] CRAN (R 4.3.1) #> knitr 1.44 2023-09-11 [1] CRAN (R 4.3.0) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.3.0) #> lubridate * 1.9.3 2023-09-27 [1] CRAN (R 4.3.1) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.0) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.3.0) #> nycflights13 * 1.0.2 2021-04-12 [1] CRAN (R 4.3.0) #> pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.0) #> purrr * 1.0.2 2023-08-10 [1] CRAN (R 4.3.0) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.3.0) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.3.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.3.0) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.3.0) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.0) #> readr * 2.1.4 2023-02-10 [1] CRAN (R 4.3.0) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.3.0) #> rlang 1.1.1 2023-04-28 [1] CRAN (R 4.3.0) #> rmarkdown 2.25 2023-09-18 [1] CRAN (R 4.3.1) #> scales 1.2.1 2022-08-20 [1] CRAN (R 4.3.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.0) #> stringi 1.7.12 2023-01-11 [1] CRAN (R 4.3.0) #> stringr * 1.5.0 2022-12-02 [1] CRAN (R 4.3.0) #> styler 1.10.2 2023-08-29 [1] CRAN (R 4.3.0) #> tibble * 3.2.1 2023-03-20 [1] CRAN (R 4.3.0) #> tidyr * 1.3.0 2023-01-24 [1] CRAN (R 4.3.0) #> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.3.0) #> tidyverse * 2.0.0 2023-02-22 [1] CRAN (R 4.3.0) #> timechange 0.2.0 2023-01-11 [1] CRAN (R 4.3.0) #> tzdb 0.4.0 2023-05-12 [1] CRAN (R 4.3.0) #> utf8 1.2.3 2023-01-31 [1] CRAN (R 4.3.0) #> vctrs 0.6.4 2023-10-12 [1] CRAN (R 4.3.1) #> withr 2.5.1 2023-09-26 [1] CRAN (R 4.3.1) #> xfun 0.40 2023-08-09 [1] CRAN (R 4.3.0) #> yaml 2.3.7 2023-01-23 [1] CRAN (R 4.3.0) #> #> [1] /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```