I have an association that I have already sorted on "EVDATE"
and then grouped by some Keys. In this case "CONTID"
and "OZONE"
A subset of the result is something like this:
data={
<|"CONTID" -> 47001805, "EVDATE" -> {2017, 1, 1, 0, 4, 37.}, "OZONE" -> 50, "EVTYPE" -> 1, "ZONE" -> 50, "KEYALM" -> "E150"|>,
<|"CONTID" -> 47001805, "EVDATE" -> {2017, 1, 1, 0, 18, 53.997}, "OZONE" -> 50, "EVTYPE" -> 0, "ZONE" -> 50, "KEYALM" -> "R150"|>,
<|"CONTID" -> 47001805, "EVDATE" -> {2017, 1, 28, 14, 26, 53.}, "OZONE" -> 50, "EVTYPE" -> 1, "ZONE" -> 50, "KEYALM" -> "E150"|>,
<|"CONTID" -> 47001805, "EVDATE" -> {2017, 1, 28, 14, 51, 51.}, "OZONE" -> 50, "EVTYPE" -> 0, "ZONE" -> 50, "KEYALM" -> "R150"|>
}
I would like to regroup, partition, or select the rows that form pairs of consecutive row values of "KEYALM"
that equal "E150"
and "R150"
respectively, in that order no gaps, as many as there are in the subset, etc.
Only those that match the pattern of consecutive rows. There are some instances were the pairs are not balanced or there are two "E150"s
in a row and that is why selecting only the pairs of "E150"
, "R150"
consecutively interest me.
I have tried,
Cases[data, {___, #1[["KEYALM"]] == "E150", #2[["KEYALM"]] == "R150", __} &]
as well as,
SequenceCases[data, {#1[["KEYALM"]] == "E150", #2[["KEYALM"]] == "R150"} &]
as well as,
Gather[data, #1[["KEYALM"]] == "E150" === #2[["KEYALM"]] == "R150" &]
Gather
gets close but isn't correct.
the correct answer should be:
res={
{
<|"CONTID" -> 47001805, "EVDATE" -> {2017, 1, 1, 0, 4, 37.`}, "OZONE" -> 50, "EVTYPE" -> 1, "ZONE" -> 50, "KEYALM" -> "E150"|>,
<|"CONTID" -> 47001805, "EVDATE" -> {2017, 1, 1, 0, 18, 53.997`}, "OZONE" -> 50, "EVTYPE" -> 0, "ZONE" -> 50, "KEYALM" -> "R150"|>
}, {
<|"CONTID" -> 47001805, "EVDATE" -> {2017, 1, 28, 14, 26, 53.`}, "OZONE" -> 50, "EVTYPE" -> 1, "ZONE" -> 50, "KEYALM" -> "E150"|>,
<|"CONTID" -> 47001805, "EVDATE" -> {2017, 1, 28, 14, 51, 51.`}, "OZONE" -> 50, "EVTYPE" -> 0, "ZONE" -> 50, "KEYALM" -> "R150"|>
}}
Thank you.