Subset data.table by logical column

后端 未结 3 2186
猫巷女王i
猫巷女王i 2020-12-03 09:51

I have a data.table with a logical column. Why the name of the logical column can not be used directly for the i argument? See the example.

相关标签:
3条回答
  • 2020-12-03 10:38

    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.

    So dt[x] will try to evaluate x in the calling scope (in this case the global environment)

    You can get around this by using ( or { or force

    dt[(x)]
    dt[{x}]
    dt[force(x)]
    
    0 讨论(0)
  • 2020-12-03 10:38

    This should also work and is arguably more natural:

    setkey(dt, x)
    dt[J(TRUE)]
    dt[J(FALSE)]
    
    0 讨论(0)
  • 2020-12-03 10:41

    x is not defined in the global environment. If you try this,

    > with(dt, dt[x])
          x y
    1: TRUE 1
    2: TRUE 2
    3: TRUE 4
    

    It would work. Or this:

    > attach(dt)
    > dt[!x]
           x y
    1: FALSE 3
    

    EDIT:

    according to the documentation the j parameter takes column name, in fact:

    > dt[x]
    Error in eval(expr, envir, enclos) : object 'x' not found
    > dt[j = x]
    [1]  TRUE  TRUE FALSE  TRUE
    

    then, the i parameter takes either numerical or logical expression (like x itself should be), however it seems it (data.table) can't see x as logical without this:

    > dt[i = x]
    Error in eval(expr, envir, enclos) : object 'x' not found
    > dt[i = as.logical(x)]
          x y
    1: TRUE 1
    2: TRUE 2
    3: TRUE 4
    
    0 讨论(0)
提交回复
热议问题