Getting the set of indices where pval<0.05
is easy, the question is how to get the indices within 2 of these. One possibility is this:
> indices<-c(5,9,13,17,25)
> indices2<-sort(unique(as.vector(outer(indices,c(-2,-1,1,2),`+`))))
> indices2
[1] 3 4 6 7 8 10 11 12 14 15 16 18 19 23 24 26 27
So putting it all together in your case would look like this:
> dt[sort(unique(as.vector(outer(which(dt$pval<0.05),c(-2,-1,1,2),`+`)))),]
SNP pval GO
4 4 0.3 D
5 5 0.9 L
7 7 0.6 L
8 8 0.1 G