Can I use tidyselect or tidyeval in count()?

I'd like to give dplyr::count() a list of variables to count by. None of these work:

llibrary(tidyverse)
library(palmerpenguins)


varlist = c("species", "island")

penguins %>% 
  count(varlist)

penguins %>% 
  count(all_of(varlist))

penguins %>% 
  count(!!varlist)

penguins %>% 
  count(!!!varlist)

penguins %>% 
  count({{varlist}})

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

Session info
sessionInfo()
#> R version 4.3.0 (2023-04-21)
#> Platform: x86_64-apple-darwin20 (64-bit)
#> Running under: macOS 14.0
#> 
#> Matrix products: default
#> BLAS:   /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRblas.0.dylib 
#> LAPACK: /Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0
#> 
#> locale:
#> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
#> 
#> time zone: Europe/London
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] palmerpenguins_0.1.1 lubridate_1.9.2      forcats_1.0.0       
#>  [4] stringr_1.5.0        dplyr_1.1.2          purrr_1.0.1         
#>  [7] readr_2.1.4          tidyr_1.3.0          tibble_3.2.1        
#> [10] ggplot2_3.4.2        tidyverse_2.0.0     
#> 
#> loaded via a namespace (and not attached):
#>  [1] gtable_0.3.3     compiler_4.3.0   reprex_2.0.2     tidyselect_1.2.0
#>  [5] scales_1.2.1     yaml_2.3.7       fastmap_1.1.1    R6_2.5.1        
#>  [9] generics_0.1.3   knitr_1.43       munsell_0.5.0    pillar_1.9.0    
#> [13] tzdb_0.3.0       rlang_1.1.1      utf8_1.2.3       stringi_1.7.12  
#> [17] xfun_0.39        fs_1.6.3         timechange_0.2.0 cli_3.6.1       
#> [21] withr_2.5.0      magrittr_2.0.3   digest_0.6.33    grid_4.3.0      
#> [25] rstudioapi_0.14  hms_1.1.3        lifecycle_1.0.3  vctrs_0.6.3     
#> [29] evaluate_0.21    glue_1.6.2       fansi_1.0.4      colorspace_2.1-0
#> [33] rmarkdown_2.23   tools_4.3.0      pkgconfig_2.0.3  htmltools_0.5.5

Thanks very much in advance for any suggestions!

count(penguins, !!!syms(varlist))
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.