Skip to content

Error message should remind correct usage of indexing with logical vector column #1844

@dracodoc

Description

@dracodoc

It's pretty common in R to use a logical vector for selecting, but this fails:

> x <- data.table(b = c(FALSE, TRUE), d = 1:2)
> x
       b d
1: FALSE 1
2:  TRUE 2
> x[b]
Error in eval(expr, envir, enclos) : object 'b' not found
> x[(b)]
      b d
1: TRUE 2

This is a frequent mistake, see here and here

The problem can be avoided with more user friendly error message. For example:

Error in eval(expr, envir, enclos) : object 'b' not found
When 'i' is a single variable name, it is not considered an expression of column names and is instead evaluated in calling scope. 
If 'b' is intended to be used as logical vector to index, wrap 'b' with () as expression.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions