Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while self indexing in data.table

Tags:

r

data.table

Here is an example of what I mean which makes things clear:

require(data.table)
x = data.table(a=1:10, idx=sample(c(TRUE, FALSE), 10, replace=TRUE))
x[idx]
Error in eval(expr, envir, enclos) : object 'idx' not found

However, the following works:

x[idx[]]
#a  idx
#1:  2 TRUE
#2:  5 TRUE
#3:  7 TRUE
#4:  9 TRUE
#5: 10 TRUE

Any idea what's happening here?

like image 570
Alex Avatar asked Mar 19 '26 11:03

Alex


1 Answers

Quoting from the link provided in the comments by @GSee.

Hi, Yes expected. From ?data.table: "Advanced: When i is a single variable name, it is not considered an expression of column names and is instead evaluated in calling scope." Subsetting by a logical column is the only example I can think of where this is confusing. But we make use of this feature quite a lot e.g. TMP=list(...);DT[TMP] safe in the knowledge that DT[TMP] won't start to fail if DT in future has a column called TMP. When I have a logical column boolCol I wrap with (): DT[(boolCol)].
This avoids the memory allocation and scan of ==TRUE, and avoids the variable name repetition of DT[DT$boolCol] Matthew

like image 168
Alex Avatar answered Mar 22 '26 02:03

Alex