I want to create a function where the first input of create_df
gets turned into the number 1 using tidyeval. Here it should be only the cyl
column.
How do I pull the first "input" of ...
?
library(dplyr, quietly = T)
create_df <- function(...){
var <- enquos(...)
first <- as_label(quos(...))
mtcars %>%
group_by(!!!var) %>%
summarise(mean = mean(mpg)) %>%
mutate(!!first := 1)
}
create_df(cyl, am)
#> `summarise()` regrouping output by 'cyl' (override with `.groups` argument)
#> # A tibble: 6 x 4
#> # Groups: cyl [3]
#> cyl am mean `<quos>`
#> <dbl> <dbl> <dbl> <dbl>
#> 1 4 0 22.9 1
#> 2 4 1 28.1 1
#> 3 6 0 19.1 1
#> 4 6 1 20.6 1
#> 5 8 0 15.0 1
#> 6 8 1 15.4 1
Created on 2020-07-01 by the reprex package (v0.3.0)