# Multiple columns from a single column of data file

Suppose I have a data file containing one column of 1000 rows having x, y, z and ϕ values. I need to separate them into four distinct columns by selecting every alternative value.

For example, if I have a column such as {a,b,c,d,e,f,g,h,i,j}, then I need to select {a,c,e,g,i} for one column and {b,d,f,h,j} for another column.

z = RandomReal[{0, 1}, 1000]


How do I do that?

• If crange = CharacterRange["a", "z"] then consider: (t0 = Partition[crange, 4]) // Column or t1 = Transpose@Partition[crange, 4]. I am not sure which one you want. Do you want to rewrite this file back as a four column file? If so, can you include a smaller sample file with perhaps 100 entries.
– Syed
Jun 24, 2022 at 18:36
• Yes, actually, the data file contains a single column having x,y,z, and phi values. I need to separate them into four distinct columns Jun 24, 2022 at 18:44
• @user84456 From your last description, then would a simple Partition[z, 4] work? Jun 24, 2022 at 19:04
• If Partition produces a 'ragged' array, perhaps you require Flatten[Partition[{a,b,c,d,e,f,g,h,i,j,k},UpTo[2]],{{2}}] (which in this case gives {{a, c, e, g, i, k}, {b, d, f, h, j}}) Jun 24, 2022 at 22:28

If I understand what you seek, then with z = Range[32] for clarity, maybe:

First@ Multicolumn[z, {4, Automatic}]

(* Out:
{{1, 5, 9, 13, 17, 21, 25, 29},
{2, 6, 10, 14, 18, 22, 26, 30},
{3, 7, 11, 15, 19, 23, 27, 31},
{4, 8, 12, 16, 20, 24, 28, 32}}
*)


First save a list of random numbers to a file to create a mock experiment:

alist = RandomReal[{0, 1}, 100]
Export["C:/alist.txt", alist, "List"]


Then read it back like so:

t1=ReadList["C:/alist.txt", {Number, Number, Number, Number}]


Export it back like this:

Export["C:/alist2.txt", t1, "Table"]


Try Table and List as formats to see the difference.