Example data:
ID <- c('A','A','A','A','A','B','B','B','B','C','C','C','C')
Hour <- c('0','2','5','6','9','0','2','5','6','0','5','6','9')
Intensity <- as.numeric(c('220','192','180','175','140','227','193','163','144','232','205','190','185'))
x <- data.frame(ID, Hour, Intensity)
x
ID Hour Intensity
1 A 0 220
2 A 2 192
3 A 5 180
4 A 6 175
5 A 9 140
6 B 0 227
7 B 2 193
8 B 5 163
9 B 6 144
10 C 0 232
11 C 5 205
12 C 6 190
13 C 9 185
I want to remove all rows associated with an ID where there are non-consecutive values of Hour, according to this list:
uniqueHoursOrder <- sort(unique(Hour))
uniqueHoursOrder
[1] "0" "2" "5" "6" "9"
I want to include any ID so long as it has a row for the first value of uniqueHoursOrder (i.e. 0) and it's other rows follow in order according to the order of uniqueHoursOrder. It's OK if an ID doesn't have a row for every value of Hour in uniqueHoursOrder.
For this data, the result should be:
ID Hour Intensity
1 A 0 220
2 A 2 192
3 A 5 180
4 A 6 175
5 A 9 140
6 B 0 227
7 B 2 193
8 B 5 163
9 B 6 144
(ID C is excluded because it's missing Hour 2. B is included because it has consecutive values of Hour starting with 0, even though it doesn't have rows for Hour for all values in uniqueHoursOrder.)
A dplyr solution would be ideal, but I'll take any help I can get.