0

The data.table package in R only lets me select rows with the [] operator using one simple logical test at a time. I can't use && or || operators, even though logically equivalent ifelse statements work as expected. Is there any good reason for this restriction? It has easy, more verbose workarounds like ifelse (and method chaining to simulate &&), but it's still irritating.

Here's a transcript of an illustrative terminal session.

R version 3.3.2 (2016-10-31) -- "Sincere Pumpkin Patch"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-unknown-cygwin (64-bit)
# --- Rest of startup message snipped. ---
> library(data.table)
data.table 1.10.4
# --- Rest of startup message snipped. ---
> dt <- data.table(a=1:6, b=11:16) # Make a sample data table.
> dt # Print the data table, for easy reference.
   a  b
1: 1 11
2: 2 12
3: 3 13
4: 4 14
5: 5 15
6: 6 16
> dt[a >= 3] # A single logical test works fine.
   a  b
1: 3 13
2: 4 14
3: 5 15
4: 6 16
> dt[((a >= 3) && (b <= 15))] # Conjunction with && returns zero rows.
Empty data.table (0 rows) of 2 cols: a,b
> dt[ifelse(a >= 3, b <= 15, FALSE)] # The equivalent ifelse call works.
   a  b
1: 3 13
2: 4 14
3: 5 15
> dt[((a <= 2) || (b >= 15))] # Disjunction with || returns every row.
   a  b
1: 1 11
2: 2 12
3: 3 13
4: 4 14
5: 5 15
6: 6 16
> dt[ifelse(a <= 2, TRUE, b >= 15)] # The equivalent ifelse call works.
   a  b
1: 1 11
2: 2 12
3: 5 15
4: 6 16
Connor Harris
  • 421
  • 5
  • 14

0 Answers0