emacselisporg-mode

How to transpose a table after import it in an org file automatically?


I read a table from a txt file as follows and how do I transpose it by script? It seems that org-table-transpose-table-at-point function could not be used in the script. Are there any built-in functions to shorten the script?

#+begin_src emacs-lisp
  (org-table-import "ttt.out" nil)
#+end_src

The file ttt.out will be:

#Cluster   Frames   Rep RepScore   Rep RepScore
       0      23      2      0.6     6      0.4 
       1      10      5      0.1     3      0.5
       2       5      4      0.4     5      0.6
       3       4      1      0.4     2      0.6

The results I need (transpose).

| #Cluster |   0 |   1 |   2 |   3 |
| Frames   |  23 |  10 |   5 |   4 |
| Rep      |   2 |   5 |   4 |   1 |
| RepScore | 0.6 | 0.1 | 0.4 | 0.4 |
| Rep      |   6 |   3 |   5 |   2 |
| RepScore | 0.4 | 0.5 | 0.6 | 0.6 |

Extra

The transpose could use org-babel from (https://lists.gnu.org/archive/html/emacs-orgmode/2010-04/msg00239.html)

However, it did not work as follows:

(apply #'mapcar* #'list (org-table-import "ttt.out" nil))

What happened?


Solution

  • In the link you provide the table is passed in to babel block as a variable. (org-table-import "ttt.out" nil) doesn't return anything so the apply has nothing to work on.

    Here is one solution for you:

    #+BEGIN_SRC emacs-lisp :results org
    (with-temp-buffer "tmp.org"
              (org-table-import "ttt.out" nil)
              (org-table-transpose-table-at-point)
              (buffer-string))
    #+END_SRC
    

    That returns the table as as string, and you could insert it where you want.

    (assuming you want emacs-lisp here)