Quantcast
Channel: How to avoid ellipsis ... in dplyr? - Stack Overflow
Viewing all articles
Browse latest Browse all 4

How to avoid ellipsis ... in dplyr?

$
0
0

I want to create a function that takes a grouping argument. Which can be a single or multiple variables. I want it to look like this:

wanted <- function(data, groups, other_params){  data %>% group_by( {{groups}} ) %>% count()}

This work only when a single group is given but breaks when there are multiple groups. I know it's possible to use the following with ellipsis ... (But I want the syntax groups = something):

not_wanted <- function(data, ..., other_params){  data %>% group_by( ... ) %>% count()}

Here is the entire code:

library(dplyr)library(magrittr)iris$group2 <- rep(1:5, 30)wanted <- function(data, groups, other_params){  data %>% group_by( {{groups}} ) %>% count()}not_wanted <- function(data, ..., other_params){  data %>% group_by( ... ) %>% count()}# workswanted(iris, groups = Species )not_wanted(iris, Species, group2)# doesn't workwanted(iris, groups = vars(Species, group2) )wanted(iris, groups = c(Species, group2) )wanted(iris, groups = vars("Species", "group2") )#  Error: Column `vars(Species, group2)` must be length 150 (the number of rows) or one, not 2

Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>