The standard solution would be to use key (/.):
+//./ |: arr

The three slashes in a row make this a bit confusing, but it is equivalent
to
({."1 (+/)/. {:"1) arr
where key is the really important part.

Marshall

-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of Ian Clark
Sent: Tuesday, May 17, 2011 5:09 PM
To: Programming forum
Subject: [Jprogramming] Aggregation

I'm being lazy here. But I need a better answer than I can devil out myself.
It occurs in a recent submission to Vector...

I have an array like this (which may be unsorted, and can grow very large):

1       100
1       100
1       20
1       400
2       30
2       200
2       300
33      100
33      100
33      100

I want to collapse it to:

1       620
2       530
33      300

i.e. summing over subheadings.
The original example had A B C  in place of 1 2 33, but numbers will do, to
save boxing. We don't know the full set of A B C ... in advance. Nothing to
be assumed about the first column, except it is
+ve integers. But I'm also interested in the case where the first
column lies in the set: i.(n) for some n>0. In other words they can be
squashed up.

1. There's simply got to be a "jem" to do it. Suggestions, please.
        - Transpose the array if you wish. Box it: 1 100 ; 1 100 ; 1 20 ;
...
-whatever.
        - No, of course I don't want a looping solution :)

2. What do you call this process? I call it "aggregation" -- but I think the
name differs across disciplines.
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to