3

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 !

Julien Navarre
  • 7,653
  • 3
  • 42
  • 69
  • 1
    btw, you don't need the group_by. That's what count is for. – talat Jul 07 '15 at 13:22
  • Yes I just realize it, reading the ?count help. Thanks ! – Julien Navarre Jul 07 '15 at 13:24
  • 1
    `iris %>% group_by("Species") %>% count_("x = Species")` looks like a bug and shouldn't work IMO. It also doesn't work if you run this line second time. Also, any reason you using `count_` instead of `count`? – David Arenburg Jul 07 '15 at 14:12
  • Yes you are right, it doesn't always work. I use SE version of functions because this is not for an "interactive use", I'm trying to wrap them in my own function so the variable to count should be passed as an argument. – Julien Navarre Jul 07 '15 at 14:22

2 Answers2

2

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

count_(iris, setNames("Species", "type"))
Julien Navarre
  • 7,653
  • 3
  • 42
  • 69
0

Here is a way to do it :

 iris %>% 
  rename(Type=Species) %>%
  count_("Type") 
Andrelrms
  • 819
  • 9
  • 13