emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] Determine min/max values in a table


From: Adam Porter
Subject: Re: [O] Determine min/max values in a table
Date: Thu, 03 Aug 2017 04:53:32 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (gnu/linux)

Thierry Banel <address@hidden> writes:

> Alternatively you have the orgtbl-aggregate package available on Melpa.
>
> #+BEGIN: aggregate :table "myvalues" :cols "min(Values) max(Values)
> mean(Values)"
>
> | min(Values) | max(Values) | mean(Values) |
> |-------------+-------------+--------------|
> |           2 |           7 |          4.5 |

Wow, that's very cool!  Had no idea about that package.

Karl, if that doesn't work for you, you might look at the
org-table-to-lisp function.  Here's an example of a function that uses
it to sum columns in the current region:

#+BEGIN_SRC elisp
(defun org-fitness-sum-table-lines ()
    "Sum each numeric column in table lines touched by the region."
    (interactive)
    (org-with-wide-buffer
     (let* (
            ;; Add empty column because (org-table-get-specials) leaves the 
empty one out, which throws off the indices
            (header (cons nil (org-table-column-names)))
            (start (save-excursion
                     (goto-line (line-number-at-pos (region-beginning)))
                     (line-beginning-position)))
            (end (save-excursion
                   (goto-line (line-number-at-pos (region-end)))
                   (line-end-position)))
            (lines (buffer-substring-no-properties start end))
            (table (--remove (equal 'hline it)
                             (org-table-to-lisp lines)))
            (indices (cdr  ; Drop index representing first column, which is 
always empty
                      (butlast  ; Drop index representing last column, which is 
comments
                       (-find-indices (lambda (col)
                                        (or (string= col "")
                                            (string= col "0")
                                            (string= col "0.0")
                                            (string= col "0.00")
                                            (< 0 (string-to-number col))))
                                      (car table)))))
            (sums (cl-loop for i in indices
                           collect (-reduce '+ (-map 'string-to-number
                                                     (-select-column i 
table)))))
            (result (-zip (-select-by-indices indices header) sums)))
       (org-fitness-display-values result :prefix "Lines: "))))
#+END_SRC




reply via email to

[Prev in Thread] Current Thread [Next in Thread]