Using function aggregate() and ddply()