With dplyr starting version 0.7 the methods ending with underscore such as summarize_ group_by_ are deprecated since we are supposed to use quosures.
See: https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html
I am trying to implement the following example using quo and !!
Working example:
df <- data.frame(x = c("a","a","a","b","b","b"), y=c(1,1,2,2,3,3), z = 1:6)
lFG <- df %>%
group_by( x,y)
lFG %>% summarize( min(z))
However, in the case, I need to implement the columns to group by and summarize are specified as strings.
cols2group <- c("x","y")
col2summarize <- "z"
How can I get the same example as above working?
From dplyr
1.0.0 you can use across
:
library(dplyr)
cols2group <- c("x","y")
col2summarize <- "z"
df %>%
group_by(across(all_of(cols2group))) %>%
summarise(across(all_of(col2summarize), min)) %>%
ungroup
# x y z
# <chr> <dbl> <int>
#1 a 1 1
#2 a 2 3
#3 b 2 4
#4 b 3 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With