Take a look into group_by() and summarize() functions from dplyr package.
group_by()
summarize()
dplyr
We could give you more specific help if you turn this into a reproducible example, if you never heard of it before, here is how to do it