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