modify the mean calculation function, use aggregation instead of ddply