Skip to content

[Request] Allow rbindlist(L, idcol = v) for any vector v of matching length #2159

Closed

Description

@franknarf1

I have a list of tables (read from disk) that I want to stack with rbindlist, assigning an id column:

library(data.table)
DT = data.table(f_id = 2:3, tab = list(
    data.table(g = 1:2, v = (1:2)*10),
    data.table(g = 3:4, v = (3:4)*10)
))

tabDT = DT[, rbindlist(setNames(tab, f_id), idcol = "f_id")][, f_id := as.integer(f_id) ][]

Because my id column is not a character, I need to type it four (!) times and manually coerce it back to the correct class. There are other ways to handle it, but all the ones I can think of are quite clunky, like...

tabDT = DT[, rbindlist(tab, idcol = TRUE)][, `:=`(f_id = DT$f_id[.id], .id = NULL)][]

So I'd like syntax like this instead:

DT[, rbindlist(tab, idcol = f_id, idcol.name = "f_id")]
# or
DT[, rbindlist(tab, idcol = list(f_id = f_id))]
# or
DT[, rbindlist(tab, idcol = f_id)]
# with the name automatically inferred in the last case

Activity

  1. MichaelChirico commented on May 10, 2017

    @MichaelChirico
    Member

    Agree with this. I swore I had already put up this FR in fact!

  2. arunsrinivasan commented on Jun 29, 2017

    @arunsrinivasan
    Member

    This'd be better implemented with a separate function like unwrap(), for example. I remember having an implementation somewhere.. will have to check old local branches..

  3. arunsrinivasan commented on Jun 29, 2017

    @arunsrinivasan
    Member

    Marking this as a dup of #2146. If you feel strongly that rbindlist is the way to go, feel free to reopen and give your thoughts.

  4. franknarf1 commented on Jun 29, 2017

    @franknarf1
    ContributorAuthor

    That sounds good. I could get used to a unnest(DT[, c("f_id", "tab")], id = "f_id") idiom or whatever. (I'm not familiar with unnest syntax and always keep a single column for an ID.)

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