Skip to content

Inconsistent logical indexing behaviour #758

@mchen402

Description

@mchen402

When the i index contains a mix of T and F, surprisingly there are no less rows then the original data.table:

data.table(x = 1:2)[c(F, T), list(x, y = 3:4)]   # 2 rows returned
##    x y
## 1: 2 3
## 2: 2 4

I would have expected

##    x y
## 1: 2 4

This is at odds with data.frame intuition:

data.frame(x = 1:2)[c(F, T), c("x", "x")]  # 1 row returned
##   x x.1
## 2 2   2

Edge-cases

Moreover, the case where i = F does not even yield a valid result, which makes this an annoying edge-case to deal with:

data.table(x = 1:2)[c(F, F), list(x, y = 3:4)]
## Error in if (mn%%n[i] != 0) warning("Item ", i, " is of size ", n[i],  : 
##   missing value where TRUE/FALSE needed

I would have expected

## Empty data.table (0 rows) of 2 cols: x,y

The other edge case where all i = T does work as expected:

data.table(x = 1:2)[c(T, T), list(x, y = 3:4)]
##    x y
## 1: 1 3
## 2: 2 4

Is there any explanation behind this behaviour?

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions