emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Michael Brand <michael.ch.brand@gmail.com>
To: Stefan Huchler <stefan.huchler@mail.de>
Cc: Org Mode <emacs-orgmode@gnu.org>
Subject: Re: how to group together fields
Date: Sat, 22 Feb 2014 21:20:29 +0100	[thread overview]
Message-ID: <CALn3zojuvqgnzkW2e2B6WPpF+ipSN3gLbcAN9xZrdR=P_VoYoA@mail.gmail.com> (raw)
In-Reply-To: <8738k09q13.fsf@mars.lan>

Hi Stefan

On Mon, Feb 3, 2014 at 4:25 PM, Stefan Huchler <stefan.huchler@mail.de> wrote:
> I get with the 1. COLUMNS line something like that:
>
> Tea Shop 1     |  5.0   | ...
> Tea Shop 2     |  7.0   | ...
>
> I want another output that looks like that:
>
> Blend 1        |  6.0   | ...
> Blend 2        |  3.0   | ...

My suggestion for these several GROUP BY as in SQL on the same data is
to use Org babel with R or TBLFM.

I am not a user of the R programming language and therefore can not
provide a working example but it would be one of the simpler
solutions. Put all data into one Org table just like "#+NAME: data"
below and for each result table use an Org babel source block with R
that reads from this table.

Some R example of GROUP BY as in SQL:
http://stats.stackexchange.com/questions/8225/how-to-summarize-data-by-group-in-r

With TBLFM the only thing that comes to my mind is org-lookup-all. The
disadvantage of this solution is that you have to enumerate all groups
manually in the result tables:

#+NAME: data
| Purchase | Product | Shop | Rating |
|----------+---------+------+--------|
| a        | p1      | s1   |      1 |
| b        | p1      | s2   |      4 |
| c        | p2      | s1   |      2 |
| d        | p3      | s2   |      8 |

Product rating and ranking by average purchase:

| Product | Rating | Ranking |
|---------+--------+---------|
| p1      |    2.5 |       2 |
| p2      |    2.0 |       3 |
| p3      |    8.0 |       1 |
#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 '(remote(data,
@I$2..@>$2)) '(remote(data, @I$4..@>$4))))) (/ (apply '+ all) (length
all) 1.0)); L :: $3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2)
nil '<))); N

Shop rating and ranking by average purchase:

| Shop | Rating | Ranking |
|------+--------+---------|
| s1   |    1.5 |       2 |
| s2   |    6.0 |       1 |
#+TBLFM: $2 = '(let ((all (org-lookup-all '$1 '(remote(data,
@I$3..@>$3)) '(remote(data, @I$4..@>$4))))) (/ (apply '+ all) (length
all) 1.0)); L :: $3 = '(+ 1 (length (org-lookup-all $2 '(@I$2..@>$2)
nil '<))); N

I added the above TBLFM solution as an ERT that can also be found via
Worg: On the page with the Worg tutorials in the spreadsheet section
follow the link to the "Emacs Regression Test (ERT) for Org tables"
with example use cases for TBLFM:
http://orgmode.org/worg/org-tutorials/index.html#Spreadsheet
In the ERT it is test-org-table/org-lookup-all.

Michael

  reply	other threads:[~2014-02-22 20:20 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-30 15:45 how to group together fields Stefan Huchler
2014-01-31 11:44 ` Alexander Baier
     [not found]   ` <87r47nshuh.fsf@mars.lan>
2014-02-01 11:03     ` Alexander Baier
     [not found]       ` <87mwibrs5u.fsf@mars.lan>
2014-02-01 11:46         ` Alexander Baier
     [not found]       ` <87ioszrr6l.fsf@mars.lan>
2014-02-01 12:33         ` Alexander Baier
2014-02-01 13:02           ` Stefan Huchler
2014-02-03  8:51             ` Bastien
2014-02-03 15:25               ` Stefan Huchler
2014-02-22 20:20                 ` Michael Brand [this message]
2014-02-23  1:12                   ` Stefan Huchler
2014-02-23  7:32                     ` Michael Brand
2014-02-22 15:19   ` Stefan Huchler

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='CALn3zojuvqgnzkW2e2B6WPpF+ipSN3gLbcAN9xZrdR=P_VoYoA@mail.gmail.com' \
    --to=michael.ch.brand@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=stefan.huchler@mail.de \
    /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).