Closed davidtedfordholt closed 2 years ago
It seems worthwhile to let people group by something other than the full key structure, but that it's worth pointing out when the tsibble
is grouped and the person might be expecting the behavior of group_by_key()
.
My thoughts on behavior (am willing to do otherwise):
count(tbl_ts)
>> tbl
add_count(tbl_ts)
>> tbl_ts
tbl_ts
equivalent to input, with column n
count(grouped_ts)
>> tbl
groups(grouped_ts) != key_vars(grouped_ts)
, then use groups but message
that they aren't the same.add_count(grouped_ts)
>> grouped_ts
summarise()
and dropping one group in this case.groups(grouped_ts) != key_vars(grouped_ts)
, then use groups but message
that they aren't the same. This would lead to a tsbl
with each row including the count based on the group, not unique key combination.message
perhaps should be a warn
.Yes, these methods would be helpful. The proposed behaviours look good, but better if no message()
. PRs are welcome.
FYI, {dplyr} hasn't made count()
generic yet https://github.com/tidyverse/dplyr/issues/5538
Gotcha! I will get on it. Would you prefer I create the generic for count()
and default to dplyr::count()
, or just write it as though count()
is generic and just wait on the merge until it is?
Generic count()
and tally()
are there, so I'm working on a PR.
@earowang , would you expect a tibble
that includes both State
and Animal
from this call, or just State
?
tsibbledata::aus_livestock %>%
group_by(Animal) %>%
count(State)
I would expect one that only includes State
and n
, but I can see an argument both ways. In particular, the behavior in dplyr
is to use both in the grouping.
To be consistent with {dplyr} behaviours, I think they include Animal
and State
in the output, but keep Animal
as the grouping variable in the end.
This is especially true for logging and testing input and output data in production systems. I think
count
andtally
would returntibble
objects with ann
column, whileadd_count
andadd_tally
would return tsibbles with the same key/index structure as the input.I'm working on a PR for it at the moment, but need to make sure that it works for double-indexed tsibbles, as well as writing tests.