Skip to content

weird exception when by contains get #1985

Closed

Description

@valentas-kurauskas

Just copied my question on stackoverflow:

get function usually works without problems in data.table, but I am not able to understand the reason of this bug.

library(data.table)
tb<-data.table(x=c(1,2), y=c(3,4), z=c(5,6), w=c("a","b"))
tb[w != "b", .(x=sum(x)), by=.(y, zz=z)] #OK
#    y zz x
# 1: 3  5 1
tb[, .(x=sum(x)), by=.(y, zz=get("z"))] #OK
#    y zz x
# 1: 3  5 1
# 2: 4  6 2
tb[w != "b", .(x=sum(x)), by=.(y, zz=get("z"))] #not OK?!

Error in get("z") : object 'z' not found

(I use R version 3.3.2 and data.table version 1.9.6.)

Activity

  1. changed the title [-]weird R data.table bug when by contains get[/-] [+]weird R data.table exception when by contains get[/+] on Jan 11, 2017
  2. changed the title [-]weird R data.table exception when by contains get[/-] [+]weird exception when by contains get[/+] on Jan 11, 2017
  3. valentas-kurauskas commented on Jan 11, 2017

    @valentas-kurauskas
    Author

    This arose for me in a real but more complex setting, where I wanted to update and aggregate in one go, and pass a column name as a parameter, e.g. by=.(y=y+1, zz=get(arg)). In my opinion, it would be nice and more consistent if an exception didn't arise here.

  4. mhdann commented on Mar 18, 2020

    @mhdann

    This bug occurred for me in a real-world situation today.

    I would like to add my minimum reproducible example that I typed up before finding your post.

    library(data.table)
    dtIris <- as.data.table(iris)
    
    speciesVar <- "Species"
    
    # Working:
    head(dtIris[, .N, by = .(var = get(speciesVar), Petal.Width)])
    
    # Add  a conditional to I clause and it does not work:
    dtIris[Sepal.Length > 4, .N, by = .(var = get(speciesVar), Petal.Width)]
    
    # REMOVE one of the by list and it works again!
    dtIris[Sepal.Length > 4, .N, by = .(var = get(speciesVar))]
    
  5. jangorecki commented on Mar 19, 2020

    @jangorecki
    Member

    @mhdann thank you for providing your example, as a workaround you can currently use

    ans = dtIris[Sepal.Length > 4, .N, by = c(speciesVar, "Petal.Width")]
    setnames(ans, speciesVar, "var")

    This kind of parameter substitution is well addressed by pending PR #4304

    dtIris[Sepal.Length > 4, .N, by = .(var = .speciesVar, Petal.Width),
           env = list(.speciesVar = speciesVar)]
  6. added
    programming
    on Apr 5, 2020
  7. dshilane commented on Jan 9, 2024

    @dshilane

    Thank you for raising this issue. I can offer a few responses based on the example that @mhdann provided. First, let's start with the portion of code that leads to the error message:

    library(data.table)
    dtIris <- as.data.table(iris)
    
    speciesVar <- "Species"
    
    # Working:
    head(dtIris[, .N, by = .(var = get(speciesVar), Petal.Width)])
    
    # Add  a conditional to I clause and it does not work:
    dtIris[Sepal.Length > 4, .N, by = .(var = get(speciesVar), Petal.Width)]
    

    The last line produces the following error message:

    Error in get(speciesVar) : object 'Species' not found

    I think this error is triggering because the data.table is looking for speciesVar in the outside environment. You would trigger the same kind of error message with:

    list(get(speciesVar), dtIris[, Petal.Width])
    Error in get(speciesVar) : object 'Species' not found
    

    In addition to the suggestions above, I can offer a few other workarounds:

    dtIris[Sepal.Length > 4, .N, by = .(var = get(speciesVar), pw = get("Petal.Width"))]

    In this example, using get() statements for each of the variables in the by step ensures that both sub-objects of the list are the same length.

    Alternatively, the getDTeval package can be used to translate the get() statements before evaluation.

    library(getDTeval)
    getDTeval(the.statement = expression(dtIris[Sepal.Length > 4, .N, by = .(var = get(speciesVar), Petal.Width)]), return.as = "all")
    

    Finally, you could also work around the issue with:

    dtIris[Sepal.Length > 4, .N, by = .(var = dtIris[, get(speciesVar)], Petal.Width)]

    In this case, the by step is invoking an outside call to dtiris, which is not efficient.

  8. jangorecki commented on Jan 9, 2024

    @jangorecki
    Member

    Check out new env argument, addresses such problems in a much cleaner way

    dtIris[Sepal.Length > 4, .N, by = .(var = speciesVar, Petal.Width), env=list(speciesVar="Species")]

    you can even pass column name var as parameter as well

  9. MichaelChirico commented on Nov 8, 2026

    @MichaelChirico
    Member

    Fixed by #4981

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

    programming

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions