dt <- data.table(a=sample(c('a', 'b', 'c'), 20, replace=TRUE),
b=sample(c('a', 'b', 'c'), 20, replace=TRUE),
c=sample(20), key=c('a', 'b'))
sub <- subset(dt, a == 'a')
all.equal(key(sub), key(dt))
Run the code above in your browser using DataLab