Group rows in data frame


I have a data frame with two columns: X and Y.
The X column consist of different years, and I need to count how many rows I have per each year. For example:
year 1
year 1
year 1
year 1
year 2
year 2
year 2
year 3
year 3
The data frame is big, I don't know how many years I have. So I thought using the group_by function but I don't know how.
Finally I need to see the following output:
year 1 4
year 2 3
year 3 2

You need to combine group_by() and summarise(). Learn more about this common transformation here.

Let us know if you have still have trouble.

I've read this page but I get an error:
Error in UseMethod("group_by_") :
no applicable method for 'group_by_' applied to an object of class "factor"

Can you please post the code that generated this error?


I wanted the group_by function to group rows out of a column

Your syntax isn't right. The first argument should be the data frame followed by the grouping vars. Try group_by(only.3shot, instead.

Perfect! I succeeded grouping, but I'm not able to sum how many rows for each group.
I want to use summerise(only.3shot, ...)

Did you go through the examples on the page I linked to? The fourth example does exactly what you want. Try adapting it to your code.

Thank you very much!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.