Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format Data: Make Columns to Rows (and other way around)

Tags:

emacs

elisp

Consider this case: I have some data which looks like this:

1.9170000e+03 $ 1.6909110e+00
1.4550000e+03 $ 1.7775459e+00
1.1800000e+03 $ 1.8771469e+00
1.0000000e+03 $ 1.9992190e+00
8.7500000e+02 $ 2.1938025e+00
7.8300000e+02 $ 2.5585915e+00

Note that the dollar sign separating the two column may be any character (ranging from a certain number of spaces, the \t (tab) char, a comma or just about any character that is uniquely used for the purpose of separating column. Also note that the data may have more than two columns.

Now I'd like to reformat the block of data such that all items from a certain column are listed in a single row (separated by the char that I marked as $ in the above example): The items from column 0 fill row 0, the items from column 1 fill row 1 and so on.

Is there a predefined emacs function to do that? Or if not, is there some "self-rolled" function to achieve that?

Also I'm very interested in a function that does the reverse thing, i.e. take some lines and put them in the column structure.

Any help is much appreciated!

like image 416
elemakil Avatar asked Apr 18 '13 16:04

elemakil


People also ask

How do I rearrange data from column to row?

Here's how you can transpose cell content: Copy the cell range. Select the empty cells where you want to paste the transposed data. On the Home tab, click the Paste icon, and select Paste Transpose.


1 Answers

You can use csv-mode (available from package.el). This is a simple transpose operation. To do so customize the value of csv-separators to '("$") and then use C-c C-t.

like image 117
Nicolas Dudebout Avatar answered Oct 11 '22 09:10

Nicolas Dudebout