all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Difference between two rows
@ 2014-06-01 11:38 Cecil Westerhof
  2014-06-01 11:57 ` Michael Brand
  2014-06-01 17:57 ` Thierry Banel
  0 siblings, 2 replies; 6+ messages in thread
From: Cecil Westerhof @ 2014-06-01 11:38 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 385 bytes --]

I like to calculate in a table the difference between the value in a field
in the current row and the previous field. How would I do that?

Something like:
|-------+------------|
| value | difference |
|-------+------------|
|    12 |            |
|    15 |          3 |
|    83 |         68 |
|    87 |          4 |
|    85 |         -2 |
|-------+------------|


-- 
Cecil Westerhof

[-- Attachment #2: Type: text/html, Size: 755 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Difference between two rows
  2014-06-01 11:38 Difference between two rows Cecil Westerhof
@ 2014-06-01 11:57 ` Michael Brand
  2014-06-01 12:24   ` Cecil Westerhof
  2014-06-01 17:57 ` Thierry Banel
  1 sibling, 1 reply; 6+ messages in thread
From: Michael Brand @ 2014-06-01 11:57 UTC (permalink / raw)
  To: Cecil Westerhof; +Cc: Org Mode

Hi Cecil

On Sun, Jun 1, 2014 at 1:38 PM, Cecil Westerhof <cldwesterhof@gmail.com> wrote:
> I like to calculate in a table the difference between the value in a field
> in the current row and the previous field. How would I do that?
>
> Something like:
> |-------+------------|
> | value | difference |
> |-------+------------|
> |    12 |            |
> |    15 |          3 |
> |    83 |         68 |
> |    87 |          4 |
> |    85 |         -2 |
> |-------+------------|

For such cases I prefer vlen():

#+TBLFM: $2 = if(vlen(@II..@0) == 1, string(""), $1 - @-1$1); E

Michael

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Difference between two rows
  2014-06-01 11:57 ` Michael Brand
@ 2014-06-01 12:24   ` Cecil Westerhof
  2014-06-01 13:18     ` Michael Brand
  0 siblings, 1 reply; 6+ messages in thread
From: Cecil Westerhof @ 2014-06-01 12:24 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1594 bytes --]

2014-06-01 13:57 GMT+02:00 Michael Brand <michael.ch.brand@gmail.com>:

> On Sun, Jun 1, 2014 at 1:38 PM, Cecil Westerhof <cldwesterhof@gmail.com>
> wrote:
> > I like to calculate in a table the difference between the value in a
> field
> > in the current row and the previous field. How would I do that?
> >
> > Something like:
> > |-------+------------|
> > | value | difference |
> > |-------+------------|
> > |    12 |            |
> > |    15 |          3 |
> > |    83 |         68 |
> > |    87 |          4 |
> > |    85 |         -2 |
> > |-------+------------|
>
> For such cases I prefer vlen():
>
> #+TBLFM: $2 = if(vlen(@II..@0) == 1, string(""), $1 - @-1$1); E
>

​Combining it with your previous help I now have (not every field will be
filled, and the table is a little different):
#+TBLFM: @-I$4..@+I$4 = if("$3" == "nan", string(""), $3 - $2) ; E f-1 ::
@-I$5..@+I$5 = if(vlen(@II..@0) == 1 || "$3" == "nan", string(""), $3 -
@-1$3); E f-1​


​At the moment this is good enough. But what if I would want to know the
difference between the previous filled one, divided by the ​distance?

For example:
|-------+------------|
| value | difference |
|-------+------------|
|    12 |            |
|    15 |          3 |
|       |            |
|    83 |         34 |
|       |            |
|       |            |
|       |            |
|    87 |          1 |
|       |            |
|       |            |
|       |            |
|    85 |       -0.5 |
|       |            |
|-------+------------|

-- 
Cecil Westerhof

[-- Attachment #2: Type: text/html, Size: 2662 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Difference between two rows
  2014-06-01 12:24   ` Cecil Westerhof
@ 2014-06-01 13:18     ` Michael Brand
  2014-06-01 14:12       ` Cecil Westerhof
  0 siblings, 1 reply; 6+ messages in thread
From: Michael Brand @ 2014-06-01 13:18 UTC (permalink / raw)
  To: Cecil Westerhof; +Cc: Org Mode

Hi Cecil

On Sun, Jun 1, 2014 at 2:24 PM, Cecil Westerhof <cldwesterhof@gmail.com> wrote:
> But what if I would want to know the
> difference between the previous filled one, divided by the distance?
>
> For example:
> |-------+------------|
> | value | difference |
> |-------+------------|
> |    12 |            |
> |    15 |          3 |
> |       |            |
> |    83 |         34 |
> |       |            |
> |       |            |
> |       |            |
> |    87 |          1 |
> |       |            |
> |       |            |
> |       |            |
> |    85 |       -0.5 |
> |       |            |
> |-------+------------|

More as a demonstration of what Calc can do, divided up into some
intermediate steps:

|-------+---------------------------------------------------------|
| value | difference                                              |
|-------+---------------------------------------------------------|
|    12 |                                                         |
|    15 | [15, 12]                                                |
|       |                                                         |
|    83 | [83, nan, 15, 12]                                       |
|       |                                                         |
|       |                                                         |
|       |                                                         |
|    87 | [87, nan, nan, nan, 83, nan, 15, 12]                    |
|       |                                                         |
|       |                                                         |
|       |                                                         |
|    85 | [85, nan, nan, nan, 87, nan, nan, nan, 83, nan, 15, 12] |
|       |                                                         |
|-------+---------------------------------------------------------|
#+TBLFM: $2 = if(vlen(@-I$1..@0$1) == 1 || "$1" == "nan", string(""),
rev(@-I$1..@0$1)); E

|-------+----------------------------------------------------|
| value | difference                                         |
|-------+----------------------------------------------------|
|    12 |                                                    |
|    15 | [0, 0]                                             |
|       |                                                    |
|    83 | [0, nan, 0, 0]                                     |
|       |                                                    |
|       |                                                    |
|       |                                                    |
|    87 | [0, nan, nan, nan, 0, nan, 0, 0]                   |
|       |                                                    |
|       |                                                    |
|       |                                                    |
|    85 | [0, nan, nan, nan, 0, nan, nan, nan, 0, nan, 0, 0] |
|       |                                                    |
|-------+----------------------------------------------------|
#+TBLFM: $2 = if(vlen(@-I$1..@0$1) == 1 || "$1" == "nan", string(""),
0 * rev(@-I$1..@0$1)); E

Distance to previous element:

|-------+------------|
| value | difference |
|-------+------------|
|    12 |            |
|    15 |          1 |
|       |            |
|    83 |          2 |
|       |            |
|       |            |
|       |            |
|    87 |          4 |
|       |            |
|       |            |
|       |            |
|    85 |          4 |
|       |            |
|-------+------------|
#+TBLFM: $2 = if(vlen(@-I$1..@0$1) == 1 || "$1" == "nan", string(""),
find(0 * rev(@-I$1..@0$1), 0, 2) - 1); E

Previous element:

|-------+------------|
| value | difference |
|-------+------------|
|    12 |            |
|    15 |         12 |
|       |            |
|    83 |         15 |
|       |            |
|       |            |
|       |            |
|    87 |         83 |
|       |            |
|       |            |
|       |            |
|    85 |         87 |
|       |            |
|-------+------------|
#+TBLFM: $2 = if(vlen(@-I$1..@0$1) == 1 || "$1" == "nan", string(""),
subscr(rev(@-I$1..@0$1), find(0 * rev(@-I$1..@0$1), 0, 2)); E

Combined:

|-------+------------|
| value | difference |
|-------+------------|
|    12 |            |
|    15 |          3 |
|       |            |
|    83 |         34 |
|       |            |
|       |            |
|       |            |
|    87 |          1 |
|       |            |
|       |            |
|       |            |
|    85 |       -0.5 |
|       |            |
|-------+------------|
#+TBLFM: $2 = if(vlen(@-I$1..@0$1) == 1 || "$1" == "nan", string(""),
($1 - subscr(rev(@-I$1..@0$1), find(0 * rev(@-I$1..@0$1), 0, 2))) /
(find(0 * rev(@-I$1..@0$1), 0, 2) - 1)); E

For many here on the list it would probably be easier and more
interesting to write a TBLFM in Lisp.

Michael

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Difference between two rows
  2014-06-01 13:18     ` Michael Brand
@ 2014-06-01 14:12       ` Cecil Westerhof
  0 siblings, 0 replies; 6+ messages in thread
From: Cecil Westerhof @ 2014-06-01 14:12 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 5663 bytes --]

2014-06-01 15:18 GMT+02:00 Michael Brand <michael.ch.brand@gmail.com>:

> Hi Cecil
>
> On Sun, Jun 1, 2014 at 2:24 PM, Cecil Westerhof <cldwesterhof@gmail.com>
> wrote:
> > But what if I would want to know the
> > difference between the previous filled one, divided by the distance?
> >
> > For example:
> > |-------+------------|
> > | value | difference |
> > |-------+------------|
> > |    12 |            |
> > |    15 |          3 |
> > |       |            |
> > |    83 |         34 |
> > |       |            |
> > |       |            |
> > |       |            |
> > |    87 |          1 |
> > |       |            |
> > |       |            |
> > |       |            |
> > |    85 |       -0.5 |
> > |       |            |
> > |-------+------------|
>
> More as a demonstration of what Calc can do, divided up into some
> intermediate steps:
>
> |-------+---------------------------------------------------------|
> | value | difference                                              |
> |-------+---------------------------------------------------------|
> |    12 |                                                         |
> |    15 | [15, 12]                                                |
> |       |                                                         |
> |    83 | [83, nan, 15, 12]                                       |
> |       |                                                         |
> |       |                                                         |
> |       |                                                         |
> |    87 | [87, nan, nan, nan, 83, nan, 15, 12]                    |
> |       |                                                         |
> |       |                                                         |
> |       |                                                         |
> |    85 | [85, nan, nan, nan, 87, nan, nan, nan, 83, nan, 15, 12] |
> |       |                                                         |
> |-------+---------------------------------------------------------|
> #+TBLFM: $2 = if(vlen(@-I$1..@0$1) == 1 || "$1" == "nan", string(""),
> rev(@-I$1..@0$1)); E
>
> |-------+----------------------------------------------------|
> | value | difference                                         |
> |-------+----------------------------------------------------|
> |    12 |                                                    |
> |    15 | [0, 0]                                             |
> |       |                                                    |
> |    83 | [0, nan, 0, 0]                                     |
> |       |                                                    |
> |       |                                                    |
> |       |                                                    |
> |    87 | [0, nan, nan, nan, 0, nan, 0, 0]                   |
> |       |                                                    |
> |       |                                                    |
> |       |                                                    |
> |    85 | [0, nan, nan, nan, 0, nan, nan, nan, 0, nan, 0, 0] |
> |       |                                                    |
> |-------+----------------------------------------------------|
> #+TBLFM: $2 = if(vlen(@-I$1..@0$1) == 1 || "$1" == "nan", string(""),
> 0 * rev(@-I$1..@0$1)); E
>
> Distance to previous element:
>
> |-------+------------|
> | value | difference |
> |-------+------------|
> |    12 |            |
> |    15 |          1 |
> |       |            |
> |    83 |          2 |
> |       |            |
> |       |            |
> |       |            |
> |    87 |          4 |
> |       |            |
> |       |            |
> |       |            |
> |    85 |          4 |
> |       |            |
> |-------+------------|
> #+TBLFM: $2 = if(vlen(@-I$1..@0$1) == 1 || "$1" == "nan", string(""),
> find(0 * rev(@-I$1..@0$1), 0, 2) - 1); E
>
> Previous element:
>
> |-------+------------|
> | value | difference |
> |-------+------------|
> |    12 |            |
> |    15 |         12 |
> |       |            |
> |    83 |         15 |
> |       |            |
> |       |            |
> |       |            |
> |    87 |         83 |
> |       |            |
> |       |            |
> |       |            |
> |    85 |         87 |
> |       |            |
> |-------+------------|
> #+TBLFM: $2 = if(vlen(@-I$1..@0$1) == 1 || "$1" == "nan", string(""),
> subscr(rev(@-I$1..@0$1), find(0 * rev(@-I$1..@0$1), 0, 2)); E
>
> Combined:
>
> |-------+------------|
> | value | difference |
> |-------+------------|
> |    12 |            |
> |    15 |          3 |
> |       |            |
> |    83 |         34 |
> |       |            |
> |       |            |
> |       |            |
> |    87 |          1 |
> |       |            |
> |       |            |
> |       |            |
> |    85 |       -0.5 |
> |       |            |
> |-------+------------|
> #+TBLFM: $2 = if(vlen(@-I$1..@0$1) == 1 || "$1" == "nan", string(""),
> ($1 - subscr(rev(@-I$1..@0$1), find(0 * rev(@-I$1..@0$1), 0, 2))) /
> (find(0 * rev(@-I$1..@0$1), 0, 2) - 1)); E
>
 ​
​Looks very interesting. I have to evaluate this.


> For many here on the list it would probably be easier and more
> interesting to write a TBLFM in Lisp.
>

​I will look into that later. Would it then also be possible to fill
several fields at the same time? At the moment I use the same checks for
several fields. That is not really DRY.

-- 
Cecil Westerhof

[-- Attachment #2: Type: text/html, Size: 8130 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Difference between two rows
  2014-06-01 11:38 Difference between two rows Cecil Westerhof
  2014-06-01 11:57 ` Michael Brand
@ 2014-06-01 17:57 ` Thierry Banel
  1 sibling, 0 replies; 6+ messages in thread
From: Thierry Banel @ 2014-06-01 17:57 UTC (permalink / raw)
  To: emacs-orgmode

Le 01/06/2014 13:38, Cecil Westerhof a écrit :
> I like to calculate in a table the difference between the value in a
> field in the current row and the previous field. How would I do that?
>
> Something like:
> |-------+------------|
> | value | difference |
> |-------+------------|
> |    12 |            |
> |    15 |          3 |
> |    83 |         68 |
> |    87 |          4 |
> |    85 |         -2 |
> |-------+------------|
>
>
> -- 
> Cecil Westerhof
|   | value | difference |
|---+-------+------------|
|   |    12 |            |
| # |    15 |          3 |
| # |    83 |         68 |
| # |    87 |          4 |
| # |    85 |         -2 |
|---+-------+------------|
#+TBLFM: $3=$2-@-1$2

The first row does not have a #, and therefore it is not computed.

--
Thierry

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2014-06-01 17:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-01 11:38 Difference between two rows Cecil Westerhof
2014-06-01 11:57 ` Michael Brand
2014-06-01 12:24   ` Cecil Westerhof
2014-06-01 13:18     ` Michael Brand
2014-06-01 14:12       ` Cecil Westerhof
2014-06-01 17:57 ` Thierry Banel

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.