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

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

Related

PostgreSQL: Transforming rows into columns when more than three columns are needed

I have a table like the following one:
+---------+-------+-------+-------------+--+
| Section | Group | Level | Fulfillment | |
+---------+-------+-------+-------------+--+
| A | Y | 1 | 82.2 | |
| A | Y | 2 | 23.2 | |
| A | M | 1 | 81.1 | |
| A | M | 2 | 28.2 | |
| B | Y | 1 | 89.1 | |
| B | Y | 2 | 58.2 | |
| B | M | 1 | 32.5 | |
| B | M | 2 | 21.4 | |
+---------+-------+-------+-------------+--+
And this would be my desired output:
+---------+-------+--------------------+--------------------+
| Section | Group | Level1_Fulfillment | Level2_Fulfillment |
+---------+-------+--------------------+--------------------+
| A | Y | 82.2 | 23.2 |
| A | M | 81.1 | 28.2 |
| B | Y | 89.1 | 58.2 |
| B | M | 32.5 | 21.4 |
+---------+-------+--------------------+--------------------+
Thus, for each section and group I'd like to obtain their percents of fulfillment for level 1 and level 2. To achieve this, I've tried crosstab(), but using this function returns me an error ("The provided SQL must return 3 columns: rowid, category, and values.") because I'm using more than three columns (I need to maintain section and group as identifiers for each row). Is possible to use crosstab in this case?
Regards.
I find crosstab() unnecessary complicated to use and prefer conditional aggregation:
select section,
"group",
max(fulfillment) filter (where level = 1) as level_1,
max(fulfillment) filter (where level = 2) as level_2
from the_table
group by section, "group"
order by section;
Online example

Spotfire moving average by group

I'm using a software named spotfire in which I can set "custom expressions" with MDX. By the way, it isn't a requirement to know this software to answer this question. I expect general answers that could help other people as well even if they don't use spotfire.
I need to get the moving average for each group. I have a lot of groups, I can't make a table per group.
Below is an example of my table:
ID | GROUP | DATE | VALUE | MVG_AVG
------------------------------------------
a | A | 05/10 | 5 |
b | B | 05/10 | 4 |
c | A | 05/11 | 9 |
d | B | 05/11 | 7 |
e | B | 05/12 | 7 |
f | B | 05/13 | 7 |
g | A | 05/12 | 1 |
h | B | 05/14 | 1 |
I found the LastPeriods function, but I can't get it work for each group. I use n=3 for the function.
Here is the same table with expected results for moving average :
ID | GROUP | DATE | VALUE | MVG_AVG
------------------------------------------
a | A | 05/10 | 5 | 5 #because no previous value (for group A)
b | B | 05/10 | 4 | 4
c | A | 05/11 | 9 | 5 # =(5+9+1)/3
d | B | 05/11 | 7 | 6
e | B | 05/12 | 7 | 7
f | B | 05/13 | 7 | 5
g | A | 05/12 | 1 | 1 #because no next value (for group A)
h | B | 05/14 | 1 | 1
Here is my current custom expression in Spotfire, it doesn't take into account groups:
Sum([VALUE]) OVER (LastPeriods(3,[DATE])) / 3

Combine multiple columns to yield unique values

I'm trying to use Tableau (v10.1) to combine 5 separate columns and get a count of the distinct values for that combination. Some rows/columns are empty. For example:
+-------+-------+-------+-------+-------+
| Tag 1 | Tag 2 | Tag 3 | Tag 4 | Tag 5 |
+-------+-------+-------+-------+-------+
| A | B | C | D | E |
| B | D | E | - | - |
| - | - | - | - | - |
| E | A | - | - | - |
+-------+-------+-------+-------+-------+
I want to obtain the following in a Tableau worksheet:
+-----+-------+
| Tag | Count |
+-----+-------+
| E | 3 |
| A | 2 |
| B | 2 |
| D | 2 |
| C | 1 |
+-----+-------+
I would like to do this in Tableau (using calculated fields, etc.) and not change the original data source.
Click on the data source tab, select the five fields named Tag # and then use the pivot command to reshape the data without changing the original source

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

Numbering the rows in reverse order in an Emacs Org Mode table

I'd like to do something like this:
How to achieve a row index column in Emacs Org Mode using a Calc column rule
but I'd like the rows to be numbered in reverse order. I suspect this should be very easy, and should have something to do with #>, but e.g. $1=#>-## doesn't work.
You can try this example
| row | data |
|-----+------|
| 8 | |
| 7 | |
|-----+------|
| 6 | |
| 5 | |
| 4 | |
| 3 | 5123 |
| 2 | |
| 1 | 4234 |
#+TBLFM: $1='(- (length org-table-dlines) ##)