Counting Continuous Date Frequencies

I have data.frame in the form:

Date
2011-08-16
2011-08-17
2011-08-28
2011-09-01
2011-09-05
2011-09-06
2011-10-01
2011-10-02
2011-10-03
2011-10-04

What I would like to do is take a start counter when the dates are in order, that is, they are side by side.

In the above example, we would have 2,1,1,2,4

+3
source share
1 answer

How about this:

Make reproducible example data:

dat <- read.table(text = "2011-08-16
2011-08-17
2011-08-28
2011-09-01
2011-09-05
2011-09-06
2011-10-01
2011-10-02
2011-10-03
2011-10-04")

Get a count of the number of consecutive dates using rle, following this answer and this answer :

(rle (cumsum( c(0, diff(as.Date(dat$V1)) > 1) ) ) )$lengths
[1] 2 1 1 2 4
+5
source

All Articles