Reputation: 69
I have a dataset with 34 variables and about 25,000 observations. Each observation refers to a specific incident. Its formatted something like this:
no id date ....
1 363 006 2005-11-05
2 939 012 2007-01-23
3 541 090 2009-06-14
I'm wondering if someone could walk me through how to get the total number of incidents for each month over the 5 years contained in this dataset.
Upvotes: 3
Views: 7006
Reputation: 9405
It seems like you just want to count the number of rows for each month, if so you can just use table()
:
> #make junk data
> data <- data.frame(no=rnorm(100),id=rnorm(100),date=seq(Sys.Date()-99,Sys.Date(),by="day"))
> table(format(data$date,"%b-%Y"))
Aug-2013 Jul-2013 Nov-2013 Oct-2013 Sep-2013
31 7 1 31 30
Upvotes: 5
Reputation: 263301
tapply( dfrm$no, sub(".+-(.+)-.+", "\\1", dfrm$date), sum, na.rm=TRUE)
For month and year in MM-YYYY format use this as you category code:
.... , sub("(.+)-(.+)-.+", "\\2-\\1", data$date) , ....
Upvotes: 0