Why can I filter a factor variable using a double variable in one case, but not in another?
Examples of data below:
dt <- data.table(id=1:9, var=factor(81:89)) # > dt # id var # 1: 1 81 # 2: 2 82 # 3: 3 83 # 4: 4 84 # 5: 5 85 # 6: 6 86 # 7: 7 87 # 8: 8 88 # 9: 9 89
Why does it work ...
dt[id %in% 1:7 & var %in% c(82, 84)] # id var # 1: 2 82 # 2: 4 84
... but does this give an error?
dt[var %in% c(82, 84)]
It seems like this is a little inconsistent and could be a mistake?
r data.table
Bram visser
source share