emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* return column from table as a column
@ 2021-08-13 14:17 Roger Mason
  2021-08-13 14:30 ` Greg Minshall
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Roger Mason @ 2021-08-13 14:17 UTC (permalink / raw)
  To: org-mode-email

Hello,

I need to extract a column from a table to use as input to a source
block.  I want the extracted column to be returned as a column but it is
returned as a row.  The following illustrates the problem:

#+name: s1
| scale |  scale1 |   scale3 |  jid |
| -     | 1.00402 | 0.952329 | 1632 |
| -     | 1.00402 | 0.962247 | 1633 |

#+begin_src emacs-lisp :var data=s1[,3]
data
#+end_src

#+RESULTS:
| jid | 1632 | 1633 |

I want:

|  jid |
| 1632 |
| 1633 |

Is there some means of changing 'data=s1[,3]' to accomplish this?

Thanks,
Roger

GNU Emacs 27.2 (build 1, amd64-portbld-freebsd11.4, X toolkit, cairo
version 1.16.0, Xaw3d scroll bars)

Org mode version 9.2.3 (release_9.2.3-390-gfb5091 @
/home/rmason/.emacs.d/org-git/lisp/)


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: return column from table as a column
  2021-08-13 14:17 return column from table as a column Roger Mason
@ 2021-08-13 14:30 ` Greg Minshall
  2021-08-13 16:24   ` Roger Mason
  2021-08-13 18:47   ` Greg Minshall
  2021-08-13 14:47 ` Juan Manuel Macías
  2021-08-14 12:10 ` Maxim Nikulin
  2 siblings, 2 replies; 10+ messages in thread
From: Greg Minshall @ 2021-08-13 14:30 UTC (permalink / raw)
  To: Roger Mason; +Cc: org-mode-email

Roger,

> Is there some means of changing 'data=s1[,3]' to accomplish this?

there may be some more formal way (and i think some of the
data.frame.alternative packages, like data.table:: or dplyr::, have
their own ways), but you might try transposing

> t(data=s1[,3])

cheers, Greg


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: return column from table as a column
  2021-08-13 14:17 return column from table as a column Roger Mason
  2021-08-13 14:30 ` Greg Minshall
@ 2021-08-13 14:47 ` Juan Manuel Macías
  2021-08-13 16:32   ` Roger Mason
  2021-08-14 12:10 ` Maxim Nikulin
  2 siblings, 1 reply; 10+ messages in thread
From: Juan Manuel Macías @ 2021-08-13 14:47 UTC (permalink / raw)
  To: Roger Mason; +Cc: orgmode

Hi Roger,

It's a dirty solution, but you can try:

#+name: s1
| scale |  scale1 |   scale3 |  jid |
| -     | 1.00402 | 0.952329 | 1632 |
| -     | 1.00402 | 0.962247 | 1633 |

#+begin_src emacs-lisp :var data=s1 col=3
    (let* ((column (mapcar (lambda (r) (format "%s" (nth col r))) data)))
	   (mapcar 'list column))
#+end_src

#+RESULTS:
|  jid |
| 1632 |
| 1633 |

Best regards,

Juan Manuel 

Roger Mason writes:

> Hello,
>
> I need to extract a column from a table to use as input to a source
> block.  I want the extracted column to be returned as a column but it is
> returned as a row.  The following illustrates the problem:
>
> #+name: s1
> | scale |  scale1 |   scale3 |  jid |
>
> | -     | 1.00402 | 0.952329 | 1632 |
> | -     | 1.00402 | 0.962247 | 1633 |
>
> #+begin_src emacs-lisp :var data=s1[,3]
> data
> #+end_src
>
> #+RESULTS:
> | jid | 1632 | 1633 |
>
> I want:
>
> |  jid |
> | 1632 |
> | 1633 |
>
> Is there some means of changing 'data=s1[,3]' to accomplish this?
>
> Thanks,
> Roger
>
> GNU Emacs 27.2 (build 1, amd64-portbld-freebsd11.4, X toolkit, cairo
> version 1.16.0, Xaw3d scroll bars)
>
> Org mode version 9.2.3 (release_9.2.3-390-gfb5091 @
> /home/rmason/.emacs.d/org-git/lisp/)
>



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: return column from table as a column
  2021-08-13 14:30 ` Greg Minshall
@ 2021-08-13 16:24   ` Roger Mason
  2021-08-13 18:47   ` Greg Minshall
  1 sibling, 0 replies; 10+ messages in thread
From: Roger Mason @ 2021-08-13 16:24 UTC (permalink / raw)
  To: org-mode-email

Hello Greg,

Greg Minshall writes:

> Roger,
>
>> Is there some means of changing 'data=s1[,3]' to accomplish this?
>
> there may be some more formal way (and i think some of the
> data.frame.alternative packages, like data.table:: or dplyr::, have
> their own ways), but you might try transposing
>
>> t(data=s1[,3])

I think you are implying that I should do this in R.  Please correct me
if I'm wrong.  I was hoping to avoid using a call to an external
library.

Thanks,
Roger


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: return column from table as a column
  2021-08-13 14:47 ` Juan Manuel Macías
@ 2021-08-13 16:32   ` Roger Mason
  2021-08-13 17:12     ` Juan Manuel Macías
  0 siblings, 1 reply; 10+ messages in thread
From: Roger Mason @ 2021-08-13 16:32 UTC (permalink / raw)
  To: orgmode


Hello Juan,

Juan Manuel Macías writes:

> #+name: s1
> | scale |  scale1 |   scale3 |  jid |
> | -     | 1.00402 | 0.952329 | 1632 |
> | -     | 1.00402 | 0.962247 | 1633 |
>
> #+begin_src emacs-lisp :var data=s1 col=3
>     (let* ((column (mapcar (lambda (r) (format "%s" (nth col r))) data)))
> 	   (mapcar 'list column))
> #+end_src
>
> #+RESULTS:
> |  jid |
> | 1632 |
> | 1633 |

Thank you, I think I may be able to get this to work.

Roger


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: return column from table as a column
  2021-08-13 16:32   ` Roger Mason
@ 2021-08-13 17:12     ` Juan Manuel Macías
  2021-08-14 10:30       ` Roger Mason
  0 siblings, 1 reply; 10+ messages in thread
From: Juan Manuel Macías @ 2021-08-13 17:12 UTC (permalink / raw)
  To: Roger Mason; +Cc: orgmode

Hello Roger,

Roger Mason writes:

> Thank you, I think I may be able to get this to work.

You're welcome. Just a minor fix: although the code works fine,
naturally the asterisk in `let' was unnecessary. This is a new version
with the code explained, in case you find it useful:

#+begin_src emacs-lisp :var data=s1 col=3
  (let* (
	 ;; return a list from elemens in column number `col'
	 (list-from-column (mapcar (lambda (r) (format "%s" (nth col r))) data))
	 ;; make a list of lists = a new table that contains one single column
	 (new-table (mapcar 'list list-from-column)))
    new-table)
#+end_src

Regards,

Juan Manuel 



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: return column from table as a column
  2021-08-13 14:30 ` Greg Minshall
  2021-08-13 16:24   ` Roger Mason
@ 2021-08-13 18:47   ` Greg Minshall
  1 sibling, 0 replies; 10+ messages in thread
From: Greg Minshall @ 2021-08-13 18:47 UTC (permalink / raw)
  Cc: Roger Mason, org-mode-email

> > t(data=s1[,3])

(sorry, i thought we were in R-land... :(


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: return column from table as a column
  2021-08-13 17:12     ` Juan Manuel Macías
@ 2021-08-14 10:30       ` Roger Mason
  0 siblings, 0 replies; 10+ messages in thread
From: Roger Mason @ 2021-08-14 10:30 UTC (permalink / raw)
  To: orgmode

Hello Juan,

Juan Manuel Macías writes:

> You're welcome. Just a minor fix: although the code works fine,
> naturally the asterisk in `let' was unnecessary. This is a new version
> with the code explained, in case you find it useful:
>
> #+begin_src emacs-lisp :var data=s1 col=3
>   (let* (
> 	 ;; return a list from elemens in column number `col'
> 	 (list-from-column (mapcar (lambda (r) (format "%s" (nth col r))) data))
> 	 ;; make a list of lists = a new table that contains one single column
> 	 (new-table (mapcar 'list list-from-column)))
>     new-table)
> #+end_src

Many thanks for this.  A looong time ago I was capable of programming in
emacs-lisp.  Alas I have not maintained my skills.

Best wishes,
Roger


^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: return column from table as a column
  2021-08-13 14:17 return column from table as a column Roger Mason
  2021-08-13 14:30 ` Greg Minshall
  2021-08-13 14:47 ` Juan Manuel Macías
@ 2021-08-14 12:10 ` Maxim Nikulin
  2021-08-17 20:42   ` Roger Mason
  2 siblings, 1 reply; 10+ messages in thread
From: Maxim Nikulin @ 2021-08-14 12:10 UTC (permalink / raw)
  To: emacs-orgmode

On 13/08/2021 21:17, Roger Mason wrote:
> 
> I need to extract a column from a table to use as input to a source
> block.  I want the extracted column to be returned as a column but it is
> returned as a row.  The following illustrates the problem:
> 
> #+name: s1
> | scale |  scale1 |   scale3 |  jid |
> | -     | 1.00402 | 0.952329 | 1632 |
> | -     | 1.00402 | 0.962247 | 1633 |
> 
> #+begin_src emacs-lisp :var data=s1[,3]
> data
> #+end_src
> 
> #+RESULTS:
> | jid | 1632 | 1633 |
> 
> I want:
> 
> |  jid |
> | 1632 |
> | 1633 |
> 
> Is there some means of changing 'data=s1[,3]' to accomplish this?

Notice that you can get vertical representation of results as a list
#+begin_src emacs-lisp :results list

The following is a variation of a recipe suggested by Juan Manuel:

#+name: to-column
#+begin_src emacs-lisp :var lst=()
   (mapcar #'list lst)
#+end_src

#+begin_src emacs-lisp :var data=s1[,3] :post to-column(lst=*this*)
   data
#+end_src

To transform more than one column, the following idea may be useful

(defun rotate (list-of-lists)
   (apply #'mapcar #'list list-of-lists))

https://stackoverflow.com/questions/3513128/transposing-lists-in-common-lisp



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: return column from table as a column
  2021-08-14 12:10 ` Maxim Nikulin
@ 2021-08-17 20:42   ` Roger Mason
  0 siblings, 0 replies; 10+ messages in thread
From: Roger Mason @ 2021-08-17 20:42 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

Maxim Nikulin writes:

> Notice that you can get vertical representation of results as a list
> #+begin_src emacs-lisp :results list
>
> The following is a variation of a recipe suggested by Juan Manuel:
>
> #+name: to-column
> #+begin_src emacs-lisp :var lst=()
>    (mapcar #'list lst)
> #+end_src
>
> #+begin_src emacs-lisp :var data=s1[,3] :post to-column(lst=*this*)
>    data
> #+end_src
>
> To transform more than one column, the following idea may be useful
>
> (defun rotate (list-of-lists)
>    (apply #'mapcar #'list list-of-lists))
>
> https://stackoverflow.com/questions/3513128/transposing-lists-in-common-lisp

Thank you very much.  I should really make the effort to re-learn
e-lisp.  Not having written anything in it for 10+ years has eroded my
abilities.

Best wishes,
Roger


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-08-17 20:43 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-13 14:17 return column from table as a column Roger Mason
2021-08-13 14:30 ` Greg Minshall
2021-08-13 16:24   ` Roger Mason
2021-08-13 18:47   ` Greg Minshall
2021-08-13 14:47 ` Juan Manuel Macías
2021-08-13 16:32   ` Roger Mason
2021-08-13 17:12     ` Juan Manuel Macías
2021-08-14 10:30       ` Roger Mason
2021-08-14 12:10 ` Maxim Nikulin
2021-08-17 20:42   ` Roger Mason

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).