I am working with an event-level df on protest patterns from 2000-2017. I want to create a new df that tells me the number of individual protest events for each month over this period. I have tried to do this with the following code but have been unsuccessful. There should thus be two columns: one listing the month/year and one listing the number of protests.
dates <- as.Date(dat$Date, '%m/%d/%Y')
yr <- year(dates)
monyr <- as.yearmon(dates)
lst <- lapply(list(dates, yr, monyr), function(x)
transform(dat, Count=ave(seq_along(x), x, FUN= length)))
names(lst) <- paste0('newdf', seq_along(lst))
Here is the data:
structure(list(Date = c("16-Mar-07", "19-Mar-07", "20-Mar-07",
"21-Mar-07", "21-Mar-07", "22-Mar-07"), Region = c("Kemerovo",
"Moscow City", "Saratov", "Novosibirsk", "Omsk", "Tyva")), row.names = c(NA,
6L), class = "data.frame")
Thanks for your help.