Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 206278

Is there a way in R to group by 'runs'?

$
0
0

Say I have:

df<-data.frame(group=c(1, 1, 1, 1, 2, 2, 2, 2), 
               date=c("2000-01-01", "2000-01-02", "2000-01-04", "2000-01-05", "2000-01-09", "2000-01-10", "2000-01-11", "2000-01-13"),
               want_group=c(1, 1, 2, 2, 3,3,3,4))

I want to create a want_group variable that groups by date, group, and whether they were "daily". So for example I want to create unique id's for within group 1 for the 1st and 2nd, and then a new unique id for the 4th and 5th, and then similarly for group 2 for the 9th, 10th, and 11th.

  group       date want_group
1     1 2000-01-01          1
2     1 2000-01-02          1
3     1 2000-01-04          2
4     1 2000-01-05          2
5     2 2000-01-09          3
6     2 2000-01-10          3
7     2 2000-01-11          3
8     2 2000-01-13          4

Thanks,


Viewing all articles
Browse latest Browse all 206278

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>