Skip to content

If first setkey and use by, is it acting the same as we use keyby in data.table #4320

Closed

Description

@hope-data-science

The DT[I,J,BY] is quite perfect for data.table, but keyby is breaking such structure. And my question is as the title, if I never use keyby, and just use setkey/setkeyv and then use by as before, am I getting the exact performance of using keyby?

Thanks.

Activity

  1. jangorecki commented on Mar 26, 2020

    @jangorecki
    Member

    No, you are not getting exact performance. It may be faster or slower depending on your use cases. If you just query once then keyby is likely to be faster, if multiple times then setkey first might be better. In most cases I would expect those differences to be small enough to really care about that, but as always, it is best to measure or your use case, and on your data.

  2. hope-data-science commented on Mar 26, 2020

    @hope-data-science
    Author

    So the situation is, whether I need the group computation multiple times. As stated in the help:

    keyby: Same as by, but with an additional setkey() run on the by columns of the result, for convenience. It is common practice to use 'keyby=' routinely when you wish the result to be sorted.

    I think it leads me to think keyby is a by and then use setkey. However, I perceive that data.table will do it in sorting order (first keyby) and rearrange back to original order.

    Moreover, keyby is not operation by reference, but setkey is (of course). Is that correct?

  3. jangorecki commented on Mar 26, 2020

    @jangorecki
    Member

    I believe this manual entry simplifies the process to more easily explain its behaviour, by showing how it could be treated from the outside. Internally it is not just setkey at the end of grouping.

    Yes, that is correct, keyby will produce new data.table, setkey will update existing object.

  4. hope-data-science commented on Mar 26, 2020

    @hope-data-science
    Author

    Thank you for your professional feedbacks which solves all my puzzles. Looking forward to the next release of data.table~

  5. jangorecki commented on Mar 26, 2020

    @jangorecki
    Member

    I made it little bit more clear in #4321

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions