4

How can I get below result with org table formula? The number of rows maybe too many, so doing it line by line is not good way.

For example:

@1$2 = @1$1 - @1$1
@2$2 = @2$1 - @1$1
@3$2 = @3$1 - @2$1
...

| x | delta |
|---+-------|
| 2 |     0 |
| 3 |     1 |
| 5 |     2 |
| 1 |    -4 |
| 7 |     6 |

2 Answers 2

9

Using Calc’s logical operations to handle the first row:

| x | delta |
|---+-------|
| 2 |     0 |
| 3 |     1 |
| 5 |     2 |
| 1 |    -4 |
| 7 |     6 |
#+tblfm: $2=@# < 3 ? 0 : $1-@-1$1

Explanation:

  • Org-tables understand Calc expressions (see (info "(org) Formula syntax for Calc")). The if expression uses the syntax condition ? then-part : else-part.
  • @#, the current row's index number, is being substituted before calc-eval. @# < 3 tests whether the current row's index is less than 3.
  • $1 refers to the current row in column 1. @-1$1 takes the value in column 1 with current row's index minus 1 (the cell one row above). The else part subtracts the value in the cell above from the value in the current row's cell.
1
  • 1
    Can you please add some notes about your formula. It is super crisp (+1 for that) and would be nice to know how to works!
    – Prasanna
    Commented Apr 26, 2017 at 1:43
5

As follows:

| x | delta |
|---+-------|
| 2 |     0 |
| 3 |     1 |
| 5 |     2 |
| 1 |    -4 |
| 7 |     6 |
#+TBLFM: @2$2=0::@3$2..@>$2=@0$1-@-1$1

The entry @2$2, row 2 column 2, has to be set separately. Otherwise @3$2..@>$2, rows 3 to last, column 2, is set to @0$1 (current row, column 1) minus @-1$1 (previous row, column 1). Details of mechanisms for referencing different cells may be found in the Org Mode info page, under The spreadsheet, available via C-h i or at the online version

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.