Dplyr: add_tally or add_count feature to specify the new variable name

Created on 29 Jul 2017  路  5Comments  路  Source: tidyverse/dplyr

Feature request:
When we use add_tally or add_count it generates a new variable named "n". It would be nice to have an argument where we could specify the new variable name. I think the code would be clearer.

Thanx

Most helpful comment

My two cents, since I encounter this issue often:

I agree that it is unnecessary for count() and tally() where only one count column is generated.

However for add_count(), given that often multiple count columns are added, being able to name the new column would be much cleaner, since ending up with n, nn, nnn is no good, and having to do a

df %>%
add_count(x) %>%
add_count(y) %>%
rename(countx = n,
county = nn)

is prone to breakage since n/nn is dependent on order (and even doing renames in between seems messy).

naming the output of add_count() like add_count(countx = x) doesn't seem overly complicated.

All 5 comments

Maybe it'd make sense to allow :=?

Would be nice if this option could be extended to count() and tally() as well.

I think count() and tally() are already sufficiently complicated. If you want to specify the name, just do the summary yourself.

My two cents, since I encounter this issue often:

I agree that it is unnecessary for count() and tally() where only one count column is generated.

However for add_count(), given that often multiple count columns are added, being able to name the new column would be much cleaner, since ending up with n, nn, nnn is no good, and having to do a

df %>%
add_count(x) %>%
add_count(y) %>%
rename(countx = n,
county = nn)

is prone to breakage since n/nn is dependent on order (and even doing renames in between seems messy).

naming the output of add_count() like add_count(countx = x) doesn't seem overly complicated.

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

Was this page helpful?
0 / 5 - 0 ratings