3

I have a 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 run count when Dates occur in order i.e. they are side by side.

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Dave
  • 2,386
  • 1
  • 20
  • 38

1 Answers1

5

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 the run count 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
Community
  • 1
  • 1
Ben
  • 41,615
  • 18
  • 132
  • 227
  • Thank you. Didn't know about diff! You don't need cumsum for this though. – Dave May 03 '12 at 18:41
  • @davewolfs I'm not sure what you mean about not needing `cumsum`. If I leave it out like so: `(rle (( c(0, diff(as.Date(dat$V1)) > 1) ) ) )$lengths` then I get something different to what you want: `[1] 2 3 1 1 3` – Ben May 04 '12 at 02:55
  • `runs <- rle(c(1,diff(as.Date(dat$Date)) > 1)) table(runs$lengths[runs$values == 0])` – Dave May 09 '12 at 00:56