Skip to content

Returning only groups #1269

Description

@arunsrinivasan

From tidyverse/dplyr#1073 (comment)

require(data.table)
DT = data.table(x=1:2, y=1:6)
#    x y
#1: 1 1
#2: 2 2
#3: 1 3
#4: 2 4
#5: 1 5
#6: 2 6
DT[, .SD, by=x] # rows for each group are collected together
#    x y
#1: 1 1
#2: 1 3
#3: 1 5
#4: 2 2
#5: 2 4
#6: 2 6

Currently, there's no way of returning just the groups:

# expected result
#    x
#1: 1
#2: 2

Activity

  1. kevinushey commented on Aug 14, 2015

    @kevinushey
    Contributor

    Would it be sensible to have DT[, list(), by = x] perform this kind of grouping; that is, as a special case where j is an empty list? Or is it expected that this would provide results identical to DT[, c(), by = x] in the development version of data.table?

  2. kevinushey commented on Aug 14, 2015

    @kevinushey
    Contributor

    Or, alternatively, the syntax DT[, by = x] could implicitly collapse over the grouping variable x? Right now it seems like that is a no-op.

  3. arunsrinivasan commented on Aug 14, 2015

    @arunsrinivasan
    MemberAuthor

    @kevinushey, IIRC we'd like for DT[, , by = .] and DT[by=.] to mimic DT[, .SD, by=.]. Just because it makes more sense than just returning the groups (for data.table syntax).

    DT[, .(), by=.] and DT[, list(), by=.] doesn't feel quite right to me, yet, as well. It seems like a very rare operation, but we should be able to do this. Will keep thinking. More suggestions of course welcome!

  4. eantonya commented on Aug 14, 2015

    @eantonya
    Contributor

    Related issue - #1242

  5. jangorecki commented on Aug 14, 2015

    @jangorecki
    Member

    It is possible using DT[, unique(.SD), .SDcols=.]. The DT[, list(), by=.] would be fine but the j of length 0 makes some inconsistency with non-0 rows returned value. Maybe DT[, list(.), by=.] would be better? currently just duplicates the column(s), that could be handled.

  6. kevinushey commented on Aug 14, 2015

    @kevinushey
    Contributor

    Some other thoughts:

    • DT[, NULL, by = x]: Treat NULL specially when a group has been specified?
    • DT[, .NULL, by = x]: Use a special dot symbol as a proxy for 'no column(s) to generate', similar in spirit to the other .I variables?
    • DT[, groups = x]: Add a new parameter that performs grouping and returns the grouped data.table; operates the same as by but does not accept a j expression?
  7. eantonya commented on Aug 14, 2015

    @eantonya
    Contributor

    Of the options so far, I like DT[, .(), by = ...] the most - and the more I stare at it, the more it seems to be perfectly suited for this job.

  8. vspinu commented on Aug 27, 2015

    @vspinu

    I like DT[, .(), by = ...]

    +1, seems as the most natural option to me as well.

  9. added this to the v1.9.8 milestone on Sep 22, 2015
  10. modified the milestones: , v1.9.8 on Mar 6, 2016
  11. geneorama commented on Mar 28, 2016

    @geneorama

    Personally, I think DT[keyby=...] is most natural and easiest to read (by and keyby should be interchangeable here).

    Since you don't want j, leaving it out seems natural. Also, it leaves open the possibility for adding an i argument, which could be handy. (For example you could want "all names / email combos that didn't get a response, except known spam addresses.)

  12. mattdowle commented on Mar 28, 2016

    @mattdowle
    Member
  13. jangorecki commented on Apr 13, 2016

    @jangorecki
    Member

    I like the @geneorama proposal, there is another issue on handling that #1105

  14. jangorecki commented on Aug 2, 2016

    @jangorecki
    Member

    Could eventually be defined as .SDcols=NULL, .SDcols=character().

  15. aradev commented on Nov 16, 2017

    @aradev

    Actually, there is a simple syntax for doing this:
    DT[, .(x = unique(x))]

  16. MichaelChirico commented on Nov 16, 2017

    @MichaelChirico
    Member

    @aradev In current implementation, this is much slower than DT[ , TRUE, by = x].

  17. removed this from the milestone on May 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions