Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Given a list of (frencuently repeated and unsorted) pairs

{Mode, SubMode}

I want to form the list

{ {COUNT_mode1, COUNT_mode2,...,COUNT_modeN}, {COUNT_mode1_submode1, COUNT_mode1,submode2, ... , COUNT_mdoe1,submodeM1, ... ,COUNT_modeN_submodeNM}, {mode1,mode2,...}, {submode1,submode2,...} }

Here is an example, given:

l = { {"games","minecraft"},{"games","minecraft"},{"games","WoW"}, {"books","book1"},{"books","book1"},{"books","book1"},{"books","book5"},{"other","something"},{"other","book1"}}

MyFunction[l] --> 
{
    {3,4,2},
    {2,1,3,1,1,1},
    {"games","books","other"},
    {"minecraft","WoW","book1","book5","something","book1"}
}
share|improve this question
add comment

5 Answers 5

Here is one way.

modes = Transpose[Tally[l[[All, 1]]]]

(* Out[176]= {{"games", "books", "other"}, {3, 4, 2}} *)

submodes = 
 Reverse[Transpose[
   Flatten[Map[Tally, GatherBy[l, First][[All, All, 2]]], 1]]]

(* Out[178]= {{2, 1, 3, 1, 1, 1}, {"minecraft", "WoW", "book1", "book5", 
  "something", "book1"}} *)

Riffle[modes, submodes]

(* Out[179]= {{"games", "books", "other"}, {2, 1, 3, 1, 1, 1}, {3, 4, 
  2}, {"minecraft", "WoW", "book1", "book5", "something", "book1"}} *)
share|improve this answer
add comment

If I am understanding you I think I would use:

{#2, #4, #1, Last /@ #3} & @@ Flatten[Tally /@ {First /@ l, l}, {1, 3}]
{
 {3, 4, 2},
 {2, 1, 3, 1, 1, 1},
 {"games", "books", "other"},
 {"minecraft", "WoW", "book1", "book5", "something", "book1"}
}
share|improve this answer
add comment

It clunks a bit, but it generates the specified output for the test input you provided.

summarize[pairs_] :=
 With[{gathered = GatherBy[pairs, First]},
  {Length /@ gathered,
   Flatten[Tally[#[[All, -1]]][[All, -1]] & /@ gathered],
   gathered[[All, 1, 1]],
   Flatten[DeleteDuplicates /@ gathered[[All, All, -1]], 1]
   }];

summarize[l] === {{3, 4, 2}, {2, 1, 3, 1, 1, 1}, {"games", "books", "other},  
  {"minecraft", "WoW", "book1", "book5", "something", "book1"}}

(* True *)
share|improve this answer
add comment

Here is one way.

MyFunc[l_] := Block[{t1, t2, res},
   t1 = Tally[l[[All, 1]]];
   t2 = Tally[l[[All, 2]]];
   res = {t1[[All, 2]], t2[[All, 2]], t1[[All, 1]], t2[[All, 1]]};
   Return[res];
];
share|improve this answer
    
I'd advise against using Block for variable localization. Module is much safer. –  Leonid Shifrin Apr 17 at 9:22
add comment

An approach using Reap and Sow:

Using:

lst = {{"games", "minecraft"}, {"games", "minecraft"}, {"games", 
   "WoW"}, {"books", "book1"}, {"books", "book1"}, {"books", 
   "book1"}, {"books", "book5"}, {"other", "something"}, {"other", 
   "book1"}}

Then

Join[Transpose[
   Last@Reap[Sow[1, First[#]] & /@ lst, _, {Total@#2, #1} &]],
  Transpose@
   Last@Reap[
     Sow[1, wp[#]] & /@ lst, _, {Total@#2, #1[[1, 2]]} &]][[{1, 3, 2, 
   4}]]

yields:

{{3, 4, 2}, {2, 1, 3, 1, 1, 1}, {"games", "books", 
  "other"}, {"minecraft", "WoW", "book1", "book5", "something", 
  "book1"}}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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