4
$\begingroup$

The output of MovingMap with a nested Tally will result in a list of nested lists. For example:

dex = {x, x, x, z, x, v, x, v, v, z, z, v, x, z, v, z, z, x}

sol = MovingMap[Tally, dex, Quantity[3, "Events"]]

{{{x, 3}}, {{x, 2}, {z, 1}}, {{x, 2}, {z, 1}}, {{z, 1}, {x, 1}, {v, 
   1}}, {{x, 2}, {v, 1}}, {{v, 2}, {x, 1}}, {{x, 1}, {v, 2}}, {{v, 
   2}, {z, 1}}, {{v, 1}, {z, 2}}, {{z, 2}, {v, 1}}, {{z, 1}, {v, 
   1}, {x, 1}}, {{v, 1}, {x, 1}, {z, 1}}, {{x, 1}, {z, 1}, {v, 
   1}}, {{z, 2}, {v, 1}}, {{v, 1}, {z, 2}}, {{z, 2}, {x, 1}}}

The aim is to create from list "sol" a new list containing frequency values so it would look like the following

{3,2,1,2......}

Thank you in advance for suggestions

$\endgroup$
1
  • 1
    $\begingroup$ Maybe just sol // (Nest[Map, Last, 2] /* Catenate)? $\endgroup$ Feb 5, 2019 at 20:05

3 Answers 3

1
$\begingroup$
Last /@ Flatten[sol, 1]

{3, 2, 1, 2, 1, 1, 1, 1, 2, 1, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1}

$\endgroup$
1
$\begingroup$
Join @@ MovingMap[Values@*Counts, dex, Quantity[3, "Events"]]

{3, 2, 1, 2, 1, 1, 1, 1, 2, 1, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1}

$\endgroup$
1
$\begingroup$

Here is an alternative way.

Length /@ Join @@ Gather /@ Partition[dex, 3, 1]

{3, 2, 1, 2, 1, 1, 1, 1, 2, 1, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 2, 1}

$\endgroup$

Your Answer

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

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