You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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))]
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:
Activity
[-]weird R data.table bug when by contains get[/-][+]weird R data.table exception when by contains get[/+][-]weird R data.table exception when by contains get[/-][+]weird exception when by contains get[/+]valentas-kurauskas commentedon Jan 11, 2017
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.mhdann commentedon Mar 18, 2020
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.
jangorecki commentedon Mar 19, 2020
@mhdann thank you for providing your example, as a workaround you can currently use
This kind of parameter substitution is well addressed by pending PR #4304
dshilane commentedon Jan 9, 2024
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:
The last line produces the following error message:
Error in get(speciesVar) : object 'Species' not foundI 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:
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.
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.
jangorecki commentedon Jan 9, 2024
Check out new env argument, addresses such problems in a much cleaner way
you can even pass column name
varas parameter as wellMichaelChirico commentedon Nov 8, 2026
Fixed by #4981