I have a dataset that I am processing in R. I would like to group occurences of a category over time, indicating the order at which each group appears.
Data is grouped by "id", sampled by "time", and has a "category" label (low, high).
#make example data
id <- c("a", "a", "a", "a", "a", "b", "b", "b", "b", "b")
time <- seq.POSIXt(as.POSIXct("10/24/16 21:05", format="%m/%d/%y %H:%M", tz="America/Chicago"), by="5 min", length.out=10)
category <- c("low", "high", "high", "low", "low", "low", "high", "high", "low", "low")
dat<-data.frame(id, time, category)
> dat
id time category
1 a 2016-10-24 21:05:00 low
2 a 2016-10-24 21:10:00 high
3 a 2016-10-24 21:15:00 high
4 a 2016-10-24 21:20:00 low
5 a 2016-10-24 21:25:00 low
6 b 2016-10-24 21:30:00 low
7 b 2016-10-24 21:35:00 high
8 b 2016-10-24 21:40:00 high
9 b 2016-10-24 21:45:00 low
10 b 2016-10-24 21:50:00 low
I want to create a variable "group" that marks the time-group that each category belongs to, such that: If category == category at time X and time X+1, these are in the same group If category != category at time X and time X+1, the group ends
Groups are ordered over time, such that the first occurence of a given "category" group is 1, and the next is 2.
This is different than a sequence that counts the number of occurrences of each category label over time. While I need the "group" values to be sequenced, I need the value to repeat within each "group" of sequential "category".
#example data of what I want
dat$group <- c(1, 1, 1, 2, 2, 1, 1, 1, 2, 2) #this is the variable I want
dat$seq <- with(dat, ave(as.character(category), category, FUN = seq_along)) #count sequence variable, which is different than what I'm after because it does not repeat within sequential categories
> dat
id time category group seq
1 a 2016-10-24 21:05:00 low 1 1
2 a 2016-10-24 21:10:00 high 1 1
3 a 2016-10-24 21:15:00 high 1 2
4 a 2016-10-24 21:20:00 low 2 2
5 a 2016-10-24 21:25:00 low 2 3
6 b 2016-10-24 21:30:00 low 1 4
7 b 2016-10-24 21:35:00 high 1 3
8 b 2016-10-24 21:40:00 high 1 4
9 b 2016-10-24 21:45:00 low 2 5
10 b 2016-10-24 21:50:00 low 2 6
Basically, the idea is that "group" is an event, which can take place over a varying length of time. But even if it varies in length, it's still the same event. So you have the first event, second event, etc.
I have searched online, but not seen a post that matches the question. If I have overlooked a previous post, links to relevant posts are welcome.
Thanks in advance for any help!
Edited, 12/14/2016 for clarity and to try and garner interest.