-2

I am working on a DataFrame called crime that looks like this:

X X.1 date              year  month day hour minute second
1 1 2008-08-31 20:47:00 2008     8  31   20     47      0
2 2 2008-09-01 00:45:00 2008     9   1    0     45      0
3 3 2008-09-01 03:00:00 2008     9   1    3      0      0
4 4 2008-09-09 07:46:00 2008     9   9    7     46      0

The X and X.1 attributes refer to a crime that was committed. Crime 1, crime 2, crime 3, etc., and are all distinct and unique values. That is to say that if there were 30 crimes reported by this dataset then these attributes go all the way from 1 to 30.

What I would like to do is to create a new DataFrame that has a column that reflects the count for each month and year. It would look something like this:

crimes  year  month 
10      2008     8
17      2008     9
24      2008     10
41      2008     11 

How can I do this?

pogibas
  • 27,303
  • 19
  • 84
  • 117
Guillermina
  • 3,127
  • 3
  • 15
  • 24

1 Answers1

1

You can use group_by in dplyr.

library(dplyr)

crime %>% group_by(year, month) %>% summarize(crimes = n())
Muffindorf
  • 113
  • 5