From mboxrd@z Thu Jan 1 00:00:00 1970 From: Juan Subject: Re: questions about table mode and spreadsheets Date: Mon, 6 Sep 2010 21:56:53 -0300 Message-ID: <20100907005653.GE2414@soloJazz.com> References: <4C856EB3.9060608@christianmoe.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Received: from [140.186.70.92] (port=40626 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OsmUf-0002o3-Vj for emacs-orgmode@gnu.org; Mon, 06 Sep 2010 20:57:02 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OsmUe-0000mW-I7 for emacs-orgmode@gnu.org; Mon, 06 Sep 2010 20:57:01 -0400 Received: from cpoproxy3-pub.bluehost.com ([67.222.54.6]:58047) by eggs.gnu.org with smtp (Exim 4.69) (envelope-from ) id 1OsmUe-0000m9-2i for emacs-orgmode@gnu.org; Mon, 06 Sep 2010 20:57:00 -0400 Content-Disposition: inline In-Reply-To: <4C856EB3.9060608@christianmoe.com> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Christian Moe Cc: emacs-orgmode@gnu.org, Inquisitive Scientist 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... >