Add keepby= to do what by= does now.

Aperta
#1,880 12 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
r
Ambito
data

Direzione di ricerca

Start by reviewing the current by= and keyby= grouping behavior described in the issue, including how i affects group order and why := is excluded. Resolve whether keepby= should be added alone or as part of a broader by=/keyby= migration. Done means the chosen API has defined group-order semantics and coverage for the stated cases.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement

For discussion ...

Currently by= returns the groups in the order each group first appeared. I think most often most people (including myself now) actually want the groups returned in group order by default; i.e. what keyby= does. Worse, some people think the groups are returned in random order and that that is for speed reasons; i.e. speed comes first. In fact, it actually takes data.table longer to return the groups in original order than it does to order the groups (it finds the order of the groups to find the groups in the first place rather than using hash tables). It has to do an extra step to get back to first appearance order. Of the users that have been left with the impression that data.table returns groups in random order for speed reasons, some of those then start to fear that the row order within the groups are not retained for speed reasons either. Nothing could be further from the truth.

NB1: We're not talking about the order of the rows within each group here at all. The order of the rows within each group is always retained. Always has been always will be. Set in stone. We're only talking about the order that the groups appear in the result returned. This FR doesn't apply when a := is present either; e.g. recall that the groups don't even have to be contiguous with a := by group.

NB2: When we say 'first appeared in the data' or 'order of the rows within each group' we mean after i has been applied, if present. Since DT[i, j, by] is the same as DT[i][,j,by].

Keeping the groups in first appearance order comes up for some/many users and is really important for them. In fact that's what I needed when I first created by= which is why I made that the default. But it can appear strange to others that the order that the groups first appear in the data should be relied on. Therefore, this FR is to create keepby= to do what by= does now. That way readers of the code in future will know that this query is expecting to keep first appearance order of groups retained. Retain the important ability, but make it clearer that the query uses it. I haven't seen this ability in other software so another but smaller motivating factor is to more easily explain that "data.table has keepby=".

Alternative keyword: batchby=. But to me that conveys batches defined by contiguous groups, like this answer where the same group values occur later but the user wishes them to be a separate group. For that, the new rleid() on the right hand side of by= makes most sense. That way it can be applied on a column basis.

For example, if this goes ahead, I'll change this answer to use keepby= to make it clearer to readers of that solution. That question requires something to be done depending on the previous group. See also #606.

Then over 2 years in the usual way, two options :

  1. Slowly deprecate both by= and unnamed 3rd argument inside [...]. The grouping argument would need to be explicitly named either keyby= or keepby= going forward.
    or
  2. Add option to change by= to do what keyby= does now, default FALSE. Then change default to TRUE. Then remove option. Then deprecate and remove keyby=. It would be by= or keepby= going forward. This avoids the need to explain that keyby= is a "by (which keeps the appearance order) followed by a setkey". That would be easier for new users but possibly harder migration path for existing. The default by= would be faster and more convenient in most cases. My guess is that very little existing usage relies on by= keeping group appearance order; mostly it's followed by an [order(...)] or something that doesn't depend on group appearance order. Anywhere that does rely on group appearance order should at least be changed to explicitly use keepby= to make that clearer, I'm thinking.
Lingua principale
R
Stelle
3.9k
Fork
1.1k
Merge medio
14h 4m
PR unite (30g)
4

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Rdatatable/data.table

Tutte le issue di Rdatatable/data.table

Issue simili

Altre issue su R

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.