Not sure if you know about string positions or want to look for them. In any case, this should cover both.
require(data.table) set.seed(1) DT = data.table(a=sample(1:1000,20), b=sample(1:1000,20)) setkey(DT,a) DT
Option 1: create a sequence of line numbers, combine and perform a single search in DT (no keys or binary search is necessary only for selection by line numbers):
DT[unlist(lapply(starts,seq.int,length=5))] # ab # 1: 201 267 # 2: 204 478 # 3: 266 935 # 4: 372 212 # 5: 374 711 # 6: 380 184 # 7: 491 659 # 8: 572 651 # 9: 625 863 # 10: 657 380 # 11: 760 816 # 12: 763 404 # 13: 894 385 # 14: 906 126 # 15: 940 14
Option 2: make a list of subsets of data.table, and then rbind them together. This is less effective than option 1, but for completeness:
L = lapply(starts,function(i)DT[seq.int(i,i+4)]) L
rbindlist(L)
source share