Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table formula for org spreadsheet

I have a table as follow:

| Xn | S | Pn |
| 0  | 0 | 0  |
| 1  | 0 | 1  |
| 0  | 1 | 0  |
| 1  | 1 | 0  |

I would like to search through columns Xn and S and return the value of Pn for which Xn=1 and S=0.

Can anyone advise on how I could go about doing this?

like image 202
user1185450 Avatar asked Feb 03 '12 03:02

user1185450


2 Answers

#+tblname: example-table
| Xn | S | Pn |
|  0 | 0 |  0 |
|  1 | 0 |  1 |
|  0 | 1 |  0 |
|  1 | 1 |  0 |

#+source: compute-table
#+begin_src emacs-lisp :var table=example-table
(require 'cl)
(loop for (xn s pn) in (rest table)
      when (and (= xn 1) (= s 0)) collect pn)
#+end_src

#+results: compute-table
| 1 |
like image 61
huaiyuan Avatar answered Oct 08 '22 01:10

huaiyuan


Use org-babel: Name the table and use it as input for a function that does the search in a language of your choice (out of the many languages supported by org).

In pseudo code:

#+tblname: my_table
|Xn|S|Pn|
| 0|0|9 |
[...]

#+name filter_table
#+begin_src lang :var tbl=my_table :results output
  filter tbl # tbl (my_table by default) is passed in as array of arrays (or list of lists)
  print matching Pn
#+end_src
like image 31
Tom Regner Avatar answered Oct 08 '22 00:10

Tom Regner