-
Notifications
You must be signed in to change notification settings - Fork 991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
should support DT[FLAG, ] when FLAG is a logical vector of DT #4255
Comments
DT[(FLAG)] works though; allowing DT[FLAG] opens the Pandora's box for ambiguity -- is FLAG in DT or is it in the parent frame? what if FLAG is in both DT & parent.frame? |
I use I think the data.table behavior/semantics should not be determined dynamically by whether the column exists in the data.table or it would be hard for script reader to guess the intention of the code because the reader needs to know if this column exists. If the semantics is quite static, it would be easier to manage and work with. |
Sorry, I don't get your guys' point. Even within library(data.table)
tbl <- data.table(VALUE = c(1, 0))
flag <- c(FALSE, TRUE)
tbl[(flag)]
#> VALUE
#> 1: 0
tbl[, flag := !flag]
tbl[(flag)]
#> VALUE flag
#> 1: 1 TRUE Created on 2020-02-23 by the reprex package (v0.3.0) |
Sorry for not making my point clear. My point is that we already use a lot of NSE in data table syntax by customizing the interpretation of expressions in Suppose we are only looking at the following code: tbl[flag] If the meaning or the semantics of the code depends on if By contrast, if Currently, the evaluation rule of This makes the code much easier to maintain and read in complex and collaborative data project. |
Agree with @renkun-ken -- in principle, we could examine whether The current behavior allows (with just two extra characters) the user to signal the intent that The former comes at the cost of (1) increasing the complexity of our codebase (2) introducing some assumptions which are hard to get around [i.e., suppose |
About whether
|
Thanks, I see your points. Since I always use UPPERCASE as the column and smallercase as the variable name to avoid the ambiguity, I ignored one possibility that is the By always evaluating the So I'm closing this issue. @renkun-ken @MichaelChirico Thanks for both of your answers. |
@MichaelChirico We could easily accommodate the request with adding an if else statement to the ##########see line 366 in data.table.R
# isub is a single symbol name such as B in DT[B]
i = try(eval(isub, parent.frame(), parent.frame()), silent=TRUE)
if (inherits(i,"try-error")) {
# must be "not found" since isub is a mere symbol
col = try(eval(isub, x), silent=TRUE) # is it a column name?
#########wrap with new if statement#################
if(typeof(col) != "logical") { #####new line 1
msg = if (inherits(col,"try-error")) " and it is not a column name either."
else paste0(" but it is a column of type ", typeof(col),". If you wish to select rows where that column contains TRUE",
", or perhaps that column contains row numbers of itself to select, try DT[(col)], DT[DT$col], or DT[col==TRUE] is particularly clear and is optimized.")
stop(as.character(isub), " is not found in calling scope", msg,
" When the first argument inside DT[...] is a single symbol (e.g. DT[var]), data.table looks for var in calling scope.")
} else { #####new
i = col ####new
} ####new Regarding scoping, the description is inconsistent with
library(data.table)
tbl <- data.table(FLAG = c(TRUE, FALSE), VALUE = c(1, 0))
col = "surprise"
tbl[, col := 1L][]
#> FLAG VALUE col
#> <lgcl> <num> <int>
#> 1: TRUE 1 1
#> 2: FALSE 0 1
tbl[, (col) := 1L][]
#> FLAG VALUE col surprise
#> <lgcl> <num> <int> <int>
#> 1: TRUE 1 1 1
#> 2: FALSE 0 1 1
tbl <- data.table(FLAG = c(TRUE, FALSE), VALUE = c(1, 0))
tbl[, (new_col) := 1L]
#> Error in eval(lhs, parent.frame(), parent.frame()): object 'new_col' not found |
If DT contains a logical vector (call it the
FLAG
column), we have to useDT[FLAG==TRUE]
whileDT[FLAG]
will raise errors. It's kind of confusing for the newbies.Any reason that we can't support this?
Created on 2020-02-23 by the reprex package (v0.3.0)
Session info
The text was updated successfully, but these errors were encountered: