refactor the generation of second data set using plyr package and the ddply function