Julien Navarre
Julien Navarre

Reputation: 7830

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 !

Upvotes: 3

Views: 1881

Answers (2)

Julien Navarre
Julien Navarre

Reputation: 7830

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

count_(iris, setNames("Species", "type"))

Upvotes: 2

Andrelrms
Andrelrms

Reputation: 819

Here is a way to do it :

 iris %>% 
  rename(Type=Species) %>%
  count_("Type") 

Upvotes: 0

Related Questions