and something like that: dat[dat$ID == sample(unique(dat$ID), 3), 2] ? I'm not sure about the ",2" maybe you need the full matrix ? ps: first time, i forgot the list