changed ddply usage to tapply for speed