How can I pass a character vector to dplyr::count()
.
library(magrittr)
variables <- c("cyl", "vs")
mtcars %>%
dplyr::count_(variables)
This works well, but dplyr v0.8 throws the warning:
count_() is deprecated. Please use count() instead
The 'programming' vignette or the tidyeval book can help you to program with count() : https://tidyeval.tidyverse.org
I'm not seeing standard evaluation examples of quoted names or of dplyr::count()
in https://tidyeval.tidyverse.org/dplyr.html or other chapters of the current versions of the tidyeval book and Programming with dplyr.
My two best guesses after reading this documenation and another SO question is
mtcars %>%
dplyr::count(!!variables)
mtcars %>%
dplyr::count(!!rlang::sym(variables))
which throw these two errors:
Error: Column
<chr>
must be length 32 (the number of rows) or one, not 2Error: Only strings can be converted to symbols
To create a list of symbols from strings, you want
rlang::syms
(notrlang::sym
). For unquoting a list or a vector, you want to use!!!
(not!!
). The following will work: