emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Adam Porter <adam@alphapapa.net>
To: emacs-orgmode@gnu.org
Subject: Re: Determine min/max values in a table
Date: Thu, 03 Aug 2017 04:53:32 -0500	[thread overview]
Message-ID: <87a83hou3n.fsf@alphapapa.net> (raw)
In-Reply-To: 598242B0.8010004@free.fr

Thierry Banel <tbanelwebmin@free.fr> 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	other threads:[~2017-08-03  9:53 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-08-02 12:07 Determine min/max values in a table Karl Voit
2017-08-02 14:56 ` Finding calc/elisp methods (was: Determine min/max values in a table) Karl Voit
2017-08-02 18:02   ` Finding calc/elisp methods Nick Dokos
2017-08-02 18:22   ` Adonay Felipe Nogueira
2017-08-03  9:58   ` Adam Porter
2017-08-02 21:22 ` Determine min/max values in a table Thierry Banel
2017-08-03  9:53   ` Adam Porter [this message]
2017-08-05 21:20     ` Karl Voit
     [not found] ` <c1ddc30d7673477da50fa40d6d00ac2d@HE1PR01MB1898.eurprd01.prod.exchangelabs.com>
2017-08-15 16:57   ` Finding calc/elisp methods Eric S Fraga

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87a83hou3n.fsf@alphapapa.net \
    --to=adam@alphapapa.net \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).