I'm stumped again, seeking the collective wisdom. I'm sure this is a
very common situation for those of you who work with entered data and
thus has probably been solved already long ago:
I have a table where each row has a key and a value. The rows are in
sorted order by key, and each key occurs one or more times. My goal is
to create a new table where each key appears only once, and the values
for each key are summed into a single value associated with each key.
Here's an example:
Starting condition: End result:
(t1) (t2)
+-----+--+ +-----+--+
|alpha|3 | |alpha|24|
+-----+--+ +-----+--+
|alpha|14| |beta |73|
+-----+--+ +-----+--+
|alpha|7 | |delta|75|
+-----+--+ +-----+--+
|beta |73| |gamma|22|
+-----+--+ +-----+--+
|delta|21| |omega|37|
+-----+--+ +-----+--+
|delta|15|
+-----+--+
|delta|6 |
+-----+--+
|delta|33|
+-----+--+
|gamma|5 |
+-----+--+
|gamma|17|
+-----+--+
|omega|37|
+-----+--+
I've puttered around with some previous code I had written and/or found
or obtained elsewhere and came up with the following statements, which
may or may not be useful at all:
NB. count how many elements are within each cut of unique elements:
b1=. # ;.1 ~: t1
NB. values in second column (to be summed):
c1=. _999999 ". > 1 {"1 t1
NB. nub sieve (positions of each first unique key):
d1=. ~: t1
However, I don't know where to go from here. I had originally thought
that maybe b1 and c1 could be manipulated together in some way or that
+/ could be used in some way, but I couldn't figure out anything. It
sure seems as if something ought to be possible, since this would be
such a common occurrence with gathered data. Thanks in advance for
your assistance and insights!
Harvey
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm