We can use plyr's d_ply instead of a for() loop