Skip to content

[R-Forge #5222] 'not found' when DT[, list(sum(non-.SD-col), lapply(.SD,mean)), by=..., .SDcols=...] #495

Closed

Description

@arunsrinivasan

Submitted by: Matt Weller; Assigned to: Nobody; R-Forge link

When using .SDcols (for the purpose of applying a function to multiple columns) I cannot reference other columns in the original table (v1) using the following syntax:

dt = data.table(grp=c(2,3,3,1,1,2,3), v1=1:7, v2=7:1, v3=10:16)
dt.out = dt[, c(v1 = sum(v1),  lapply(.SD,mean)), by = grp, .SDcols = v2:v3]
# Error in `[.data.table`(dt, , list(v1 = sum(v1), lapply(.SD, mean)), by = grp,  : 
#   object 'v1' not found

A similar error happens when I use c instead of list, clearly the column v1 cannot be accessed within the j clause.

I resorted to the following code which includes column v1, even though I do not want that to be included in the lapply portion, having to drop it after computation.

sd.cols = c("v1","v2", "v3")
dt.out = dt[, c(sum.v1 = sum(v1), lapply(.SD,mean)), by = grp, .SDcols = sd.cols]

According to eddi on Stackoverflow this is a bug and he has asked me to report it. I cannot provide much more detail as I'm not exactly sure which part he thinks was a bug, looking at the accepted answer by Arun and their ensuing discussion will highlight where but the problem lies.

Here is the relevant SO post.

Activity

  1. added this to the v1.9.6 milestone on Sep 24, 2014
  2. modified the milestones: v1.9.8, v1.9.6 on Oct 10, 2014
  3. arunsrinivasan commented on Jan 4, 2015

    @arunsrinivasan
    MemberAuthor
  4. MichaelChirico commented on Jul 11, 2015

    @MichaelChirico
    Member

    Bit late, but adding this question of mine to the pile

  5. jangorecki commented on Jul 11, 2015

    @jangorecki
    Member

    I didn't even think about it as a bug, usually I provide additional required fields to .SDcols and later in j I use .SD[, !"total", with=FALSE] to exclude unwanted column.

  6. MichaelChirico commented on Jul 11, 2015

    @MichaelChirico
    Member

    That's another good workaround, I wonder the performance difference vis-a-vis using dt$total. And yes, this sort of dances the line between FR and bug, IMO.

  7. DavidArenburg commented on Aug 19, 2015

    @DavidArenburg
    Member

    Bumping this up again. Looks like this could be a very important fix. this question seem to be related to and could be potentially solved via DT[, (deltaColsNewNames) := lapply(.SD, normalDelta, price), .SDcols = deltaColsNames]

  8. franknarf1 commented on Sep 10, 2015

    @franknarf1
    Contributor

    Here's another simple case where this would be useful: http://stackoverflow.com/a/32498711/1191259

  9. rentrop commented on Oct 5, 2015

    @rentrop
  10. franknarf1 commented on Oct 9, 2015

    @franknarf1
    Contributor

    Another to update when fixed: http://stackoverflow.com/q/32915770/1191259

  11. arunsrinivasan commented on Mar 7, 2016

    @arunsrinivasan
    MemberAuthor

    Yay! we can now do this:

    require(data.table)
    dt = data.table(grp=c(2,3,3,1,1,2,3), v1=1:7, v2=7:1, v3=10:16)
    dt.out = dt[, c(v1 = sum(v1),  lapply(.SD,mean)), by = grp, .SDcols = v2:v3]
      #  grp v1  v2   v3
    # 1:   2  7 4.5 12.5
    # 2:   3 12 4.0 13.0
    # 3:   1  9 3.5 13.5
  12. arunsrinivasan commented on Mar 8, 2016

    @arunsrinivasan
    MemberAuthor

    Updated all SO posts linked here. Thanks to all.

  13. DavidArenburg commented on Mar 8, 2016

    @DavidArenburg
    Member

    Thanks, @arunsrinivasan. I was waiting for this fix for couple of years.

  14. rentrop commented on Mar 8, 2016

    @rentrop

    Awesome! Thank you

  15. added a commit that references this issue on Mar 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions