0

I have a dataframe that contains a column representing the 'Year' and another column that represents 'Type':

a  Year  Creams
1  2004   11
2  2004   12
3  2001   13
4  2004   14
5  2002   15
.  ....   ..

How do I count every year in column 'Year' so it appears as:

a  Year  TypeCount
1  2004      3
2  2002      1
3  2001      1

It can be output into another dataframe, I don't mind. I just need it to be suitable to make a graph out of it at the end.

Keyan H
  • 37
  • 6
  • `df %>% group_by(Year) %>% summarise(TypeCount = n_distinct(Creams))` Or `df %>% count(Year)` if you just want to count number of rows for each `Year`. – Ronak Shah Jun 01 '20 at 04:38
  • what does %>% mean? It's not working in RStudio? Is this meant to be a substitute for something else? – Keyan H Jun 01 '20 at 04:45
  • Well, you need to load `dplyr` package for this, do it via `library(dplyr)`. There are other base R options in the links marked. – Ronak Shah Jun 01 '20 at 04:49

0 Answers0