emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Juan <Pechiar@computer.org>
To: Christian Moe <mail@christianmoe.com>
Cc: emacs-orgmode@gnu.org,
	Inquisitive Scientist <inquisitive.scientist@gmail.com>
Subject: Re: questions about table mode and spreadsheets
Date: Mon, 6 Sep 2010 21:56:53 -0300	[thread overview]
Message-ID: <20100907005653.GE2414@soloJazz.com> (raw)
In-Reply-To: <4C856EB3.9060608@christianmoe.com>

A very complex way of not adding the extra column:

| name | a  | b | c |
|------+----+---+---|
| foo  | 1  | 2 | 3 |
| bar  | 3  | 2 | 1 |
| bar  | 4  | 5 | 6 |
|------+----+---+---|
|      | 7  |   |   |
#+TBLFM: @5$2='(apply '+ (mapcar* (lambda(x y) (if (string= x "bar") y 0)) '(@I$1..@II$1) '(@I$2..@II$2)));L

 * the two arguments at the end are the name and a columns: '(foo bar bar) and '(1 3 4)
 * the lambda function returns the second argument if first is "bar", 0 otherwise.
 * mapcar* applies the lambda function to arguments from the 2 lists.
 * apply '+ adds the resulting list

Regards,
.j.

On Tue, Sep 07, 2010 at 12:44:03AM +0200, Christian Moe wrote:
> On 9/6/10 3:38 PM, Inquisitive Scientist wrote:
>
> >   2. How do I compute the sum of a column only if a corresponding row
> >matches some condition? For example, how do I compute the sum of
> >numbers in column a for which the name in column "name" is "bar"? For
> >example, I should get 7 for the sum in column a in the table below:
> >
> >| name | a | b | c |
> >|------+---+---+---|
> >| foo  | 1 | 2 | 3 |
> >| bar  | 3 | 2 | 1 |
> >| bar  | 4 | 5 | 6 |
> >|------+---+---+---|
>
> Here's one way: Add a new row after the first, as below. Then run C-c
> C-c on the formula line:
>
> | name |   | a | b | c |
> |------+---+---+---+---|
> | foo  |   | 1 | 2 | 3 |
> | bar  |   | 3 | 2 | 1 |
> | bar  |   | 4 | 5 | 6 |
> |------+---+---+---+---|
> |      |   |   |   |   |
>   #+TBLFM: $2='(if (string= $1 "bar") 1 0)::
> @5$3=vsum(vmask(@I$2..@II$2,@I..@II))
>
> It does exactly what you asked, but I don't think it will scale well...
>

  reply	other threads:[~2010-09-07  0:57 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-06 13:38 questions about table mode and spreadsheets Inquisitive Scientist
2010-09-06 22:44 ` Christian Moe
2010-09-07  0:56   ` Juan [this message]
2010-09-07  9:20     ` Christian Moe
2010-09-15 12:08       ` Inquisitive Scientist

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=20100907005653.GE2414@soloJazz.com \
    --to=pechiar@computer.org \
    --cc=emacs-orgmode@gnu.org \
    --cc=inquisitive.scientist@gmail.com \
    --cc=mail@christianmoe.com \
    /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).