You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As of dplyr 1.0.0, summarize() will create multiple rows per group, according to the length of the return value of the summary function. This new feature leads to unintended behavior if the vector return is accidental, and also can lead to data loss.
library(conflicted)
library(dplyr)
my_custom_summary_function<-function(n) {
# Should return a scalar, but I accidentally return a vector
rep(n, n)
}
tibble(n=2:0) %>%
group_by(n) %>%
summarize(out= my_custom_summary_function(n), .groups="drop") %>%
ungroup()
#> # A tibble: 3 × 2#> n out#> <int> <int>#> 1 1 1#> 2 2 2#> 3 2 2
Should we introduce a .multi = c("allow", "require", "fail") argument that supports the pre-1.0.0 strict mode of operation? Should .multi = "fail" even be the default?
library(conflicted)
library(dplyr)
my_custom_summary_function<-function(n) {
# Should return a scalar, but I accidentally return a vector
rep(n, n)
}
tibble(n=2:0) %>%
group_by(n) %>%
summarize(out= my_custom_summary_function(n), .groups="drop", .multi="fail") %>%
ungroup()
## Error: `out` has length != 1 in groups 1, 3, use `.multi = "allow"` if this is intended
I have occasionally written multi-row summarize pipelines intentionally, but when I do that I need to very carefully document what that code is doing. When I teach summarize, the working model I use is "one row per group"; otherwise it gets confused with mutate. Yes, any bugs in summary functions could corrupt data, but this behavior should be opt-in because (1) R's recycling rules make this sort of behavior easy to trigger accidentally, (2) it's hard to notice and then diagnose when it does happen, and (3) since one-row-per-group is the expected behavior, code that does something different should look like it's doing something different.
Our plan is to deprecate this behaviour in summarise() and instead introduce a new function specifically for this purpose. We just need a name. Ideas so far:
morph()
transmogrify()
multisummarise()
abridge()
remodel()
remould()
renovate()
revamp()
abridge()
shorten()
contract()
lessen()
condense()
synopsize()
I think ideally the word would be closer to summarise() than mutate(), i.e. starting later in the alphabet or ending in ise (although then we'd need UK/US variants, which isn't ideal). I think it's ok if the verb implies an unconditional shrinking, even though some uses might increase the number of rows; we also say that [ subsets a vector.
I like morph() the most out of all of these. It implies some kind of stretching/shrinking of the data without implying any direction. And it is fairly short.
Reacted by Lionel Henry, Romain François and Ryan D
morph(<grouped_df>) would have to work like summarise(<grouped_df>) currently works, I think. i.e. each group computation can return any number of rows, and we recycle the per group results "rowwise" across the resulting columns. And we'd add .by support for morph()
I'm somewhat confident we don't need to care about the direction, mainly it's:
summarise() has the guarantee of 1 row per group. More predictable for users. Harder to make a mistake. Easier data base translations.
morph() just relaxes that guarantee, but otherwise works similarly. But when you see morph() in code it should be a clear signal that something is happening that isn't a pure summary, which is pretty nice
I'm remembering that tidygraph uses morph(), which might be enough to prevent us from using it.
I also thought of restructure(), which is kind of nice because it is closer to summarise() in the alphabet and the core part of each verb starts with s (structure and summarise). And it seems to nicely convey that you are taking an existing data frame and reworking it into some new form (with little restriction on the number of rows or columns). The only potential problem is possible confusion with reshape(), but I think I'm ok with it.
It seems somewhat reasonable to say that summarise() is a restricted version / special case of restructure().
Since this operation is sometimes called "split-apply-combine", perhaps recombine, or rebuild, reconstruct, remake, or reform? Since we're making an entirely new data frame by combining the results of operations on each group.
Or, more related to existing verbs, something based on bind_rows? bind_rows_groupwise? tibble_groupwise?
With ivs, which generally takes sets of intervals and returns other sets of arbitrary size (notably can return more or less rows than you started with!)
I like it. It seems a bit too general to me though, compared to something like reframe() which is a more practical description of what is happening. But I agree that it feels more similar to mutate and summarise.
I actually liked create()because it was fairly general 😆
The fact that you can describe mutate() and summarise() using the word "create" didn't bother me too much, since their names imply they are stricter variants of it. create() is just an act of creation with the fewest restraints possible
I think create() feels a little strange because the object of the verb (as you'd use it in normal speech) is the output instead of the input. Like, you summarise()/mutate() an existing data frame but you create() a new data frame. That being said, I think it does seem to work better than the other suggestions (incl. mine above)
I like create(), but I'm afraid it sounds too magical. In my understanding, the function is rather for experts compared to mutate() and summarize() with single-row-returns, so probably it should sound more difficult.
I've been thinking about this for a few days but I haven't come up with a new name that I like, and I'm afraid I don't think any of the ones suggested here sit right with me. At the risk of being very not creative, I would suggest something like multi_summarize() or summarize_multi().
Otherwise reframe() makes the most sense but I think it won't be trivial to teach when to use reframe() vs. summarize(), as in, how will someone know they should use summarize() instead of reframe()? (Though this is maybe more of a comment on the function's functionality than its name.)
Maybe it's only me but I am not completely convinced that we need a complete new function here. I actually liked @krlmlr initial suggestion having a separate argument .multi in summarise that can define the behaviour. I can't find the discussion why that idea was rejected.
@shahronak47 the ivs examples linked here are good examples of how this typically isn't a summary operation, so summarise() isn't a good name for this. In particular, note that depending on the function you use, this operation can actually return more rows that you started with.
This verb is much more akin to do() than summarise() in my mind, even if the API itself looks a little closer to summarise() (the fact that the API looks more similar to summarise() is actually a big win in my mind over the previous do() syntax)
Activity
hadley commentedon Aug 1, 2022
I'm not too worried about this; in general any misspecified summary function could corrupt data.
DavisVaughan commentedon Aug 26, 2022
See also https://twitter.com/drob/status/1563198515626770432?s=20&t=iTFWSCPNOGWalIrpXHx2qg
kcarnold commentedon Nov 14, 2022
I have occasionally written multi-row
summarizepipelines intentionally, but when I do that I need to very carefully document what that code is doing. When I teachsummarize, the working model I use is "one row per group"; otherwise it gets confused withmutate. Yes, any bugs in summary functions could corrupt data, but this behavior should be opt-in because (1) R's recycling rules make this sort of behavior easy to trigger accidentally, (2) it's hard to notice and then diagnose when it does happen, and (3) since one-row-per-group is the expected behavior, code that does something different should look like it's doing something different.hadley commentedon Nov 17, 2022
Our plan is to deprecate this behaviour in
summarise()and instead introduce a new function specifically for this purpose. We just need a name. Ideas so far:morph()transmogrify()multisummarise()abridge()remodel()remould()renovate()revamp()abridge()shorten()contract()lessen()condense()synopsize()I think ideally the word would be closer to
summarise()thanmutate(), i.e. starting later in the alphabet or ending inise(although then we'd need UK/US variants, which isn't ideal). I think it's ok if the verb implies an unconditional shrinking, even though some uses might increase the number of rows; we also say that[subsets a vector.DavisVaughan commentedon Nov 17, 2022
I like
morph()the most out of all of these. It implies some kind of stretching/shrinking of the data without implying any direction. And it is fairly short.romainfrancois commentedon Nov 18, 2022
I like
morph()too. What will happen withmorph(<grouped_df>)?Are we considering being specific about shinking/growing ? i.e. we could have
shrink()andgrow()or something.DavisVaughan commentedon Nov 18, 2022
morph(<grouped_df>)would have to work likesummarise(<grouped_df>)currently works, I think. i.e. each group computation can return any number of rows, and we recycle the per group results "rowwise" across the resulting columns. And we'd add.bysupport formorph()I'm somewhat confident we don't need to care about the direction, mainly it's:
summarise()has the guarantee of 1 row per group. More predictable for users. Harder to make a mistake. Easier data base translations.morph()just relaxes that guarantee, but otherwise works similarly. But when you seemorph()in code it should be a clear signal that something is happening that isn't a pure summary, which is pretty niceDavisVaughan commentedon Nov 19, 2022
I'm remembering that tidygraph uses
morph(), which might be enough to prevent us from using it.I also thought of
restructure(), which is kind of nice because it is closer tosummarise()in the alphabet and the core part of each verb starts withs(structure and summarise). And it seems to nicely convey that you are taking an existing data frame and reworking it into some new form (with little restriction on the number of rows or columns). The only potential problem is possible confusion withreshape(), but I think I'm ok with it.It seems somewhat reasonable to say that
summarise()is a restricted version / special case ofrestructure().kcarnold commentedon Nov 20, 2022
Since this operation is sometimes called "split-apply-combine", perhaps
recombine, orrebuild,reconstruct,remake, orreform? Since we're making an entirely new data frame by combining the results of operations on each group.Or, more related to existing verbs, something based on
bind_rows?bind_rows_groupwise?tibble_groupwise?10 remaining items
DavisVaughan commentedon Nov 27, 2022
A few real life examples.
With ivs, which generally takes sets of intervals and returns other sets of arbitrary size (notably can return more or less rows than you started with!)
Similar idea with
intersect():Doing something silly like reproducing
slice_head()An older pattern combined with
read_csv()and multiple files, from the original dplyr 1.0.0 blog post about this featureDavisVaughan commentedon Nov 27, 2022
How about
create()?summarise()create a "new" data frame, as opposed tomutate()summarise(), because that "creates a 1 row summary from each group". So it is a special case of this.read_csv()oneWith the ivs example, I would say that I "create the groups by merging the overlapping ranges", and the code is
create(groups = iv_groups(iv)).So far this is my favorite option
Subjective reasons I like it:
mutate()andsummarise()lionel- commentedon Nov 28, 2022
I like it. It seems a bit too general to me though, compared to something like
reframe()which is a more practical description of what is happening. But I agree that it feels more similar to mutate and summarise.romainfrancois commentedon Nov 28, 2022
I like
create()and believe it would read very well with.by =wurli commentedon Nov 28, 2022
I also like
create()a lot but agree that it possibly feels overly generallionel- commentedon Nov 28, 2022
When you consider the family it's not immediately obvious why
create()is called like that because all the verbs are an act of creation:mutate()creates new columns or recreates existing columns within an existing data frame.summarise()creates a new data frame with size-1 summaries from an existing one.create()creates a new data frame from an existing one.I think this illustrates why
create()is too general.DavisVaughan commentedon Nov 28, 2022
I actually liked
create()because it was fairly general 😆The fact that you can describe
mutate()andsummarise()using the word "create" didn't bother me too much, since their names imply they are stricter variants of it.create()is just an act of creation with the fewest restraints possibleeutwt commentedon Nov 28, 2022
I think
create()feels a little strange because the object of the verb (as you'd use it in normal speech) is the output instead of the input. Like, yousummarise()/mutate()an existing data frame but youcreate()a new data frame. That being said, I think it does seem to work better than the other suggestions (incl. mine above)yutannihilation commentedon Nov 28, 2022
I like
create(), but I'm afraid it sounds too magical. In my understanding, the function is rather for experts compared tomutate()andsummarize()with single-row-returns, so probably it should sound more difficult.What about
explode(), which is used in Hive/Spark SQL? c.f. https://spark.apache.org/docs/latest/api/sql/index.html#explodemine-cetinkaya-rundel commentedon Nov 28, 2022
I've been thinking about this for a few days but I haven't come up with a new name that I like, and I'm afraid I don't think any of the ones suggested here sit right with me. At the risk of being very not creative, I would suggest something like
multi_summarize()orsummarize_multi().Otherwise
reframe()makes the most sense but I think it won't be trivial to teach when to usereframe()vs.summarize(), as in, how will someone know they should usesummarize()instead ofreframe()? (Though this is maybe more of a comment on the function's functionality than its name.)shahronak47 commentedon Nov 29, 2022
Maybe it's only me but I am not completely convinced that we need a complete new function here. I actually liked @krlmlr initial suggestion having a separate argument
.multiinsummarisethat can define the behaviour. I can't find the discussion why that idea was rejected.hadley commentedon Nov 29, 2022
@shahronak47
summarise()is a good name for this sort of operation.summarise()ends up making the call much longer.DavisVaughan commentedon Nov 29, 2022
@shahronak47 the ivs examples linked here are good examples of how this typically isn't a summary operation, so
summarise()isn't a good name for this. In particular, note that depending on the function you use, this operation can actually return more rows that you started with.This verb is much more akin to
do()thansummarise()in my mind, even if the API itself looks a little closer tosummarise()(the fact that the API looks more similar tosummarise()is actually a big win in my mind over the previousdo()syntax)