r code -- subset generation fixed