tidyeval icon indicating copy to clipboard operation
tidyeval copied to clipboard

mutate_front()

Open lionel- opened this issue 7 years ago • 0 comments

mutate_front <- function(.data, ...) {
  exprs <- enquos(..., .named = TRUE)
  .data <- mutate(.data, ...)

  new_vars <- syms(names(exprs))
  select(.data, !!!new_vars, everything())
}

starwars %>%
  mutate_front(
    height / 100,
    birth_year_fct = cut_number(birth_year, 4)
  )
#> # A tibble: 87 x 15
#>    `height/100` birth_year_fct name  height  mass hair_color skin_color
#>           <dbl> <fct>          <chr>  <int> <dbl> <chr>      <chr>
#>  1         1.72 [8,35]         Luke…    172    77 blond      fair
#>  2         1.67 (72,896]       C-3PO    167    75 NA         gold
#>  3         0.96 [8,35]         R2-D2     96    32 NA         white, bl…
#>  4         2.02 (35,52]        Dart…    202   136 none       white
#>  5         1.5  [8,35]         Leia…    150    49 brown      light
#>  6         1.78 (35,52]        Owen…    178   120 brown, gr… light
#>  7         1.65 (35,52]        Beru…    165    75 brown      light
#>  8         0.97 NA             R5-D4     97    32 NA         white, red
#>  9         1.83 [8,35]         Bigg…    183    84 black      light
#> 10         1.82 (52,72]        Obi-…    182    77 auburn, w… fair
#> # ... with 77 more rows, and 8 more variables: eye_color <chr>,
#> #   birth_year <dbl>, gender <chr>, homeworld <chr>, species <chr>,
#> #   films <list>, vehicles <list>, starships <list>

lionel- avatar Jan 07 '19 12:01 lionel-