4
$\begingroup$

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.

$\endgroup$

2 Answers 2

4
$\begingroup$

a bit brute force, but it works:

Select[  Partition[data, 2,  1] , #[[All, "KEYALM"]] == {"E150", "R150"} & ]
$\endgroup$
2
  • 1
    $\begingroup$ Partition[..,2,1] looks at every pair. This is probably slow though if its a big list. $\endgroup$
    – george2079
    Feb 16, 2017 at 21:13
  • $\begingroup$ Correct, my bad. $\endgroup$
    – Kuba
    Feb 16, 2017 at 21:15
3
$\begingroup$

I thought this will work:

SequenceCases[
 data,
 KeyValuePattern /@ {"KEYALM" -> "E150", "KEYALM" -> "R150"}
]

but it didn't (why?), so here is a workaround

data[[#]] & /@ SequencePosition[
    data[[All, "KEYALM"]]
  , {"E150", "R150"}
]
$\endgroup$
5
  • 3
    $\begingroup$ this does it SequenceCases[data, {a_ /; a[["KEYALM"]] == "E150", b_ /; b[["KEYALM"]] == "R150"}] $\endgroup$
    – george2079
    Feb 16, 2017 at 21:07
  • $\begingroup$ @george2079 yep. any idea why KeyValuePattern fails? $\endgroup$
    – Kuba
    Feb 16, 2017 at 21:16
  • $\begingroup$ Maybe it needs an Evaluate ( I don't have KeyValuePattern in 10.1 to try ) $\endgroup$
    – george2079
    Feb 16, 2017 at 21:18
  • $\begingroup$ @george2079 I don't think so, will try to investigate later. Thanks for additional example. $\endgroup$
    – Kuba
    Feb 16, 2017 at 21:19
  • $\begingroup$ I like SequenceCases[data, {a_ /; a[["KEYALM"]] == "E150", b_ /; b[["KEYALM"]] == "R150"}] as the answer. So which one should I select as the answer. I will let @george2079 and Kuba decide. Thank you for the help. $\endgroup$
    – Ray Troy
    Feb 17, 2017 at 20:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.