Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change variable name in dplyr::count using standard evaluation

How do I change the name of the grouping variable in dplyr::count_ when it's used in a standard evaluation way

For example if in the final tbl I don't want the var name "Species" but "Type" :

iris %>% 
  group_by("Species") %>% 
  count_("Species")

Source: local data frame [3 x 2]

     Species  n
1     setosa 50
2 versicolor 50
3  virginica 50

Also I wonder how dplyr::count_ works and what this expression is supposed to do ? Do you have an explanation ?

> iris %>% group_by("Species") %>% count_("x = Species")
Source: local data frame [3 x 2]

x = Species  n
1      setosa 50
2  versicolor 50
3   virginica 50

Thanks !

like image 462
Julien Navarre Avatar asked Dec 13 '25 18:12

Julien Navarre


1 Answers

Well, I used setNames before posting but in a wrong way. It seems to be the solution :

count_(iris, setNames("Species", "type"))
like image 152
Julien Navarre Avatar answered Dec 16 '25 11:12

Julien Navarre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!