Changed to use dplyr and summarize instead of tapply