Re: Request for help: working with multi-level lists
- To: mathgroup at smc.vnet.net
- Subject: [mg13679] Re: [mg13665] Request for help: working with multi-level lists
- From: BobHanlon at aol.com
- Date: Sat, 15 Aug 1998 04:39:11 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Needs["Statistics`DescriptiveStatistics`"]
theList = Table[{Random[Integer, {0, 3}], Random[],
"whatever string"}, {10}]
{{1, 0.8370020327154653, "whatever string"},
{1, 0.4401372744738867, "whatever string"},
{2, 0.9011999702854371, "whatever string"},
{3, 0.07289422506311362, "whatever string"},
{2, 0.1692284748968154, "whatever string"},
{3, 0.9612500808958513, "whatever string"},
{2, 0.3784052438692051, "whatever string"},
{2, 0.9588220015368138, "whatever string"},
{3, 0.3098155002926614, "whatever string"},
{2, 0.4220779045756698, "whatever string"}}
Mean[Transpose[Select[theList, #1[[1]] == 1 & ]][[2]]]
0.6385696535946761
Needs["Statistics`DataManipulation`"]
Mean[Column[Select[theList, #1[[1]] == 1 & ], 2]]
0.6385696535946761
Instead of using Select in the above, you can use Cases:
Cases[theList, {1, _, _}]
{{1, 0.8370020327154653, "whatever string"},
{1, 0.4401372744738867, "whatever string"}}
Or more simply
Mean[Cases[theList, {1, x_, _} -> x]]
0.6385696535946761
Bob Hanlon
In a message dated 8/11/98 6:32:41 AM, kcconnolly at AOL.com wrote:
>I have a list of 10 elements, each of which is a list of three elements
>(let's say in each case an integer, a real number, and a string). I am
>looking for the most elegant way to select those first-level elements
>(i.e., the lists) whose integer element is equal to a particular value
>(let's say "1"), and then to obtain the mean of the real number
>elements of the lists selected. This seems as if it should be simple,
>but everything I try leads to Part specification errors. Any help
>would be greatly appreciated.