0
$\begingroup$

I want to export data I collected in a Table to a CSV (or XLSX)-File. However, the output is not very handy, it is not in a column-shape but surrounded by {} and in my more complicated example, I get unexpected numbers such as -6 and a lot of spaces when I try to preview it in Excel. Furthermore, I am unable to import the data in R for further use.

My more or less reproducible example:

(* creating some data *)
fObs = Flatten[Table[{
 a,
 b,
 e,
 d,
 f,
 a,
 b,
 e,
 d,
 f,
 a,
 b,
 e,
 d,
 f,
 z}, {b, {0, .25, .50, 0.75, 2, 5, 
  36}}, {e, {0.0001, .250001, .500001, 0.750001, 1}}, {a, {2, 4.5,
   4, 7.5, 0}}, {d, {0, 3.25, 3.5, .75}}, {f, {0, 2.25, 
  2.5, .75}}, {z, {0, .25, 0.5, .75}}], 5];

Next, I want to convert the data to NumberForm (in order to get rid of fractions like 1/4, which is complicated to convert later on (if there is a better way to get rid of fractions, let me know)

fObsNo = NumberForm[fObs // N, {4, 3}]

Finally, I export

Export[NotebookDirectory[] <> "test.csv", fObsNo]

The output looks like this: output

But I expected something like this. I do not see a pattern why it sometimes gives correct output and sometimes doesn't...
enter image description here

Thanks a lot in advance!

$\endgroup$

1 Answer 1

1
$\begingroup$

Do not Export result of NumberForm, Map it instead:

Export[NotebookDirectory[] <> "test.csv", Map[NumberForm[N@#, {4, 3}] &, fObs, {2}]]
$\endgroup$
2
  • $\begingroup$ Thanks a lot! do you have an explanation for this result? $\endgroup$
    – Karl A
    Nov 13, 2019 at 15:44
  • $\begingroup$ Head[fObsNo] is NumberForm, but for proper exporting the object should be List as Head[Map[NumberForm[N@#, {4, 3}] &, fObs, {2}]] shows. With this Map we just change the appearance of each number, not the structure as the whole. $\endgroup$
    – Alx
    Nov 13, 2019 at 15:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.