Table formula for org spreadsheet - emacs

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?

#+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 |

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

Related

Boolean function for True only if last True

Is there any way how to get true only if second value is true?
| A | B | Result |
|---|---|--------|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 0 |
| 1 | 1 | 0 |
Looks like ~A & B would suffice.

Merge orgmode tables vertically

Is it possible to append a table below another? I am looking for something like this but in the following form:
#+name: tbl1
| a | 1 |
| b | 2 |
#+name: tbl2
| c | 3 |
| d | 4 |
I am expecting to get this:
| a | 1 |
| b | 2 |
| c | 3 |
| d | 4 |
From my search I found lob-tables-operations but it seems to me that it's not well documented and likely not under maintenance.
It was quite straight forward based on this example. I just used of mapcan instead of mapcar
** append tables
:PROPERTIES:
:DATE: 2015-06-19
:END:
#+name: table-names
- first-table
- second-table
- third-table
#+name: first-table
| a | 1 |
| b | 2 |
|---+---|
#+name: second-table
| c | 3 |
| d | 4 |
|---+---|
#+name: third-table
| f | 5 |
| g | 6 |
|---+---|
#+BEGIN_SRC emacs-lisp :var table-names=table-names
(mapcan #'org-babel-ref-resolve table-names)
#+END_SRC
#+RESULTS:
| a | 1 |
| b | 2 |
|---+---|
| c | 3 |
| d | 4 |
|---+---|
| f | 5 |
| g | 6 |
|---+---|

conditional sum(sumif) in org-table

I have a table like this:
#+NAME: ENTRY
|------+--------|
| Item | Amount |
|------+--------|
| A | 100 |
| B | 20 |
| A | 120 |
| C | 40 |
| B | 50 |
| A | 20 |
| C | 16 |
|------+--------|
and then I need to sum each item in another table:
#+NAME: RESULT
|------+-----|
| Item | Sum |
|------+-----|
| A | 240 |
| B | 70 |
| C | 56 |
|------+-----|
I've tried using vlookup and remote reference in this table, but I'm not able to sum the resulting list like:
#+TBLFM: $2=vsum((vconcat (org-lookup-all $1 '(remote(ENTRY,#2$1..#>$1)) '(remote(ENTRY,#2$2..#>$2)))))
But it does not give the answer.
So I have to use a place holder to hold the resulting list then sum it:
#+NAME: RESULT
|------+--------------+-----|
| Item | Placeholder | Sum |
|------+--------------+-----|
| A | [100 120 20] | 240 |
| B | [20 50] | 70 |
| C | [40 16] | 56 |
|------+--------------+-----|
#+TBLFM: $2='(vconcat (org-lookup-all $1 '(remote(ENTRY,#2$1..#>$1)) '(remote(ENTRY,#2$2..#>$2))))::$3=vsum($2)
Is there a better solution for this?
One way to do this is without vsum:
#+TBLFM: $2='(apply '+ (mapcar 'string-to-number (org-lookup-all $1 '(remote(ENTRY,#2$1..#>$1)) '(remote(ENTRY,#2$2..#>$2)))))
If you want to use a calc function, you can always use calc-eval:
#+TBLFM: $2='(calc-eval (format "vsum(%s)" (vconcat (org-lookup-all $1 '(remote(ENTRY,#2$1..#>$1)) '(remote(ENTRY,#2$2..#>$2))))))

How to compute the dot product of two column (think full column as a vector)?

gave this table:
| a | b | c |
|---+---+----+
| 3 | 4 | |
| 1 | 2 | |
| 1 | 3 | |
| 2 | 2 | |
I want to get the dot product of two column a and b ,the result should be equel to (3*4)+(1*2)+(1*3)+(2*2) which is 21.
I don't want use the clumsy formula (B1*B2+C1*C2+D1*D2+E1*E2) because actually I have a large table waiting to calculate.
I know emacs's Calc tool has a "vprod" function which can do those sort of things ,but I dont' know how to turn the full column to a vector.
Can anybody tell me how to achieve this task,appreciate it!
In emacs-calc, the simple product of 2 vectors calculates the dot product.
This works (I put the result in #6$3; also the parenthesis can be omitted):
| a | b | c |
|---+---+----|
| 3 | 4 | |
| 1 | 2 | |
| 1 | 3 | |
| 2 | 2 | |
|---+---+----|
| | | 21 |
#+TBLFM: #6$3=(#I$1..#II$1)*(#I$2..#II$2)
#I and #II span from the 1st hline to the second.
This can be solved using babel and R in org-mode:
#+name: mytable
| a | b | c |
|---+---+----+
| 3 | 4 | |
| 1 | 2 | |
| 1 | 3 | |
| 3 | 2 | |
#+begin_src R :var mytable=mytable
sum(mytable$a * mytable$b)
#+end_src
#+RESULTS:
: 23

PostgreSQL group by bug on Unicode strings?

I have a very weird thing happening, where I noticed that a group by (word) wasn't always grouping by word if that word is a UTF-8 string. In the same query, I get cases where it's been grouped correctly, and cases where it hasn't. I wonder if anybody knows what's up with that?
select *,count(*) over (partition by md5(word)) as k
from (
select word,count(*) as n
from :tmpwl
group by 1
) a order by 1,2 limit 12;
/* gives:
word | n | k
------+---+---
いい | 1 | 1
くず | 1 | 1
ごみ | 1 | 1
さま | 1 | 1
さん | 1 | 1
へま | 1 | 1
まめ | 1 | 1
よく | 1 | 1
ろく | 1 | 1
ネガ | 1 | 2 -- what the heck?
ネガ | 1 | 2
パス | 1 | 1
*/
Note that the following workaround works fine:
select word,n,count(*) over (partition by md5(word)) as k
from (
select md5(word),max(word) as word,count(*) as n
from :tmpwl
group by 1
) a order by 1,2 limit 12;
/* gives:
word | n | k
------+---+---
いい | 1 | 1
くず | 1 | 1
ごみ | 1 | 1
さま | 1 | 1
さん | 1 | 1
へま | 1 | 1
まめ | 1 | 1
よく | 1 | 1
ろく | 1 | 1
ネガ | 2 | 1
パス | 1 | 1
プア | 1 | 1
*/
The version is PostgreSQL 8.2.14 (Greenplum Database 4.0.4.0 build 3 Single-Node Edition) on x86_64-unknown-linux-gnu, compiled by GCC gcc.exe (GCC) 4.1.1 compiled on Nov 30 2010 17:20:26.
The source table :tmpwl:
\d :tmpwl
Table "pg_temp_25149.pdtmp_foo706453357357532"
Column | Type | Modifiers
----------+---------+-----------
baseword | text |
word | text |
value | integer |
lexicon | text |
nalts | bigint |
Distributed by: (word)