Reputation: 361
I have a table that has duplicate Unix time stamps in the the first column and I want to sum up the values of the second column if they have the same Unix time stamp.
|-----Time---| Value |
| 1420067592 | 23 |
| 1420076520 | 10 |
| 1420078752 | 2 |
| 1420067592 | 6 |
| 1420076520 | 4 |
| 1420078752 | 89 |
| 1420067592 | 66 |
| 1420078752 | 81 |
| 1420076520 | 15 |
The reason why I have this is I am doing multiple whisper-fetch in a code block and would like to sum up the values for the same time stamps.
Any hint how I could do this in org-mode? It would be pretty cool to produce the yearly report with Emacs :)
Upvotes: 1
Views: 175
Reputation: 361
I found an acceptable solution by using the produced table as input for another code block that's executing a Python snippet to sum up the results
#+NAME: results
| Time | Value |
|------------+-------|
| 1420067592 | 23 |
| 1420076520 | 10 |
| 1420078752 | 2 |
| 1420067592 | 6 |
| 1420076520 | 4 |
| 1420078752 | 89 |
| 1420067592 | 66 |
| 1420078752 | 81 |
| 1420076520 | 15 |
#+begin_src python :var data=results :exports results
output = {}
for line in data:
if output.get(line[0]):
output[line[0]] += line[1]
else:
output[line[0]] = line[1]
return [[x,y] for x,y in output.items()]
#+end_src
#+RESULTS:
| 1420067592 | 95 |
| 1420076520 | 29 |
| 1420078752 | 172 |
Upvotes: 3
Reputation: 111
You could use the Orgtbl-Aggregate
package available on Melpa
#+name: thetimes
| Time | Value |
|------------+-------|
| 1420067592 | 23 |
| 1420076520 | 10 |
| 1420078752 | 2 |
| 1420067592 | 6 |
| 1420076520 | 4 |
| 1420078752 | 89 |
| 1420067592 | 66 |
| 1420078752 | 81 |
| 1420076520 | 15 |
#+BEGIN: aggregate :table "thetimes" :cols "Time vsum(Value)"
| Time | vsum(Value) |
|------------+-------------|
| 1420067592 | 95 |
| 1420076520 | 29 |
| 1420078752 | 172 |
#+END:
Upvotes: 1