Re: Pure function in a pure function (again)
- To: mathgroup at smc.vnet.net
- Subject: [mg69818] Re: [mg69797] Pure function in a pure function (again)
- From: Bob Hanlon <hanlonr at cox.net>
- Date: Sat, 23 Sep 2006 23:45:19 -0400 (EDT)
- Reply-to: hanlonr at cox.net
data=Array[a,{15,5}];
Table[data[[n,1]]=Random[Integer,{1,5}],{n,Length[data]}];
Needs["Statistics`DataManipulation`"];
f[x_] := Plus@@Column[Select[data, #[[1]] == x &], 2] ;
Given your description, presumably you meant
Plus@@f/@Range[3]
a[1, 2] + a[2, 2] + a[3, 2] + a[6, 2] + a[9, 2] + a[14, 2]
This can be done without a helper function (and without the add-on package) as
Plus@@Cases[data,x_?(MemberQ[Range[3],#[[1]]]&):>x[[2]]]
a[1, 2] + a[2, 2] + a[3, 2] + a[6, 2] + a[9, 2] + a[14, 2]
%==%%
True
Bob Hanlon
---- Skirmantas Janusonis <janusonis at psych.ucsb.edu> wrote:
> Hello,
>
> I've already asked this question, but I thought there was a better way to state it. If Dat is a 2D-table, the following picks out the rows whose first element is 1,2 or 3 and adds up their the second elements:
>
> f[x_] := Plus @@ Column[Select[Dat, #[[1]] == x &], 2]
> f /@ Range[3]
>
> Instead of f/@Range[3], I'd like to use a second pure function, something like this:
>
> Map[Plus @@ Column[Select[Dat, #1[[1]] == #2 &], 2]&,Range[3]]
>
> This line is incorrect, however. How do I fix it?
>
> Thanks!
>
> Skirmantas
>