Re: Counting Categories
- To: mathgroup at smc.vnet.net
- Subject: [mg96890] Re: [mg96798] Counting Categories
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Thu, 26 Feb 2009 08:02:06 -0500 (EST)
- References: <200902250903.EAA15666@smc.vnet.net>
- Reply-to: drmajorbob at bigfoot.com
If the lists are large, I suspect Tally will be fastest. In that case, you can get the contingency table this way: colors = {red, yellow, green, blue}; sizes = {small, medium, large}; randomColor := RandomChoice@colors randomSize := RandomChoice@sizes pairs = Table[{randomColor, randomSize}, {50}]; Clear[count] count[_] = 0; Replace[Tally[pairs], {x_List, c_Integer} :> (count[x] = c), 1]; Table[count@{color, size}, {color, colors}, {size, sizes}] {{5, 6, 5}, {3, 4, 5}, {5, 4, 6}, {4, 3, 0}} The "count" function acts much like a sparse array here, without SparseArray's many limitations. Bobby On Wed, 25 Feb 2009 14:50:11 -0600, Gregory Lypny <gregory.lypny at videotron.ca> wrote: > colors = {red, yellow, green, blue}; > sizes = {small, medium, large}; > randomColor := RandomChoice@colors > randomSize := RandomChoice@sizes > pairs = Table[{randomColor, randomSize}, {50}]; > Partition[Count[pairs, #] & /@ Tuples[{colors, sizes}], Length@sizes] > {{5, 3, 3}, {3, 6, 3}, {8, 1, 2}, {5, 4, 7}} > or > Apply[Count[pairs, {##}, 1] &, Outer[List, colors, sizes], {2}] -- DrMajorBob at longhorns.com
- References:
- Counting Categories
- From: Gregory Lypny <gregory.lypny@videotron.ca>
- Counting Categories