1
$\begingroup$

I would like to export only the minimum values (here I’m finding the minimum Ra for each R0 and these minimum values are indicated in yellow cells) to a .csv file as shown below the code:

ClearAll["Global`*"]
k[R0_, m_] := R0/m;
Ra[Q_, R0_, m_] := Q (R0/m)
data = Table[{Q, R0, m, k[R0, m], Ra[Q, R0, m]}, {Q, 1, 3, 1}, {R0, 1,
     1.15, 0.05}, {m, {3.83171, 7.01559, 10.1735, 13.3237, 16.4706, 
     19.6159}}];
FormatCells[table_] := 
 With[{minPosition = Ordering[table, 1, Last[#1] < Last[#2] &]}, 
  MapAt[Item[#, Background -> Yellow] &, 
   TableForm[{#}, TableDirections -> Row, 
      TableHeadings -> {None, {"Q", "R0", "m", "k", "Ra"}}, 
      TableSpacing -> {.8, .8}] & /@ table, minPosition]]
FormatTable[table_] := Grid[FormatCells /@ table, Dividers -> All]
FormatTable[data[[1]]]
Export["Min Ra for R0=1,1.05,1.1,1.15.csv", data]

enter image description here

I tried using the export command, but that’s not how I want the output to look like. Is it possible to export the data as shown in the picture?

$\endgroup$

2 Answers 2

2
$\begingroup$

Use MinimalBy to get the correct cells. Then Export.

table = First@MinimalBy[#, Last] & /@ First@data;

Export["data.csv", table, TableHeadings -> {"Q", "R0", "m", "k", "Ra"}]
$\endgroup$
2
  • $\begingroup$ How can I export the data for different values of Q. When I change the code to FormatTable[data[[2]]], (which gives the minimum Ra for Q=2), the data in the .csv file still shows the minimum Ra for Q=0. @Domen $\endgroup$ Mar 14, 2023 at 4:55
  • 1
    $\begingroup$ Replace First@data in my code with data[[2]]. $\endgroup$
    – Domen
    Mar 14, 2023 at 10:32
0
$\begingroup$

The yellow values are

minData = data[[1, All, 6]]

Add column headers to the export

Export["minData.csv", minData // Prepend[{"Q", "R0", "m", "k", "Ra"}]]
$\endgroup$
2
  • 1
    $\begingroup$ The code will give an error because it will always export the last cell values, even if they're not minimum. @Rohit Namjoshi $\endgroup$ Mar 13, 2023 at 16:04
  • $\begingroup$ @Math_student True, you have to identify the minimum to isolate the correct element of data to export as shown by @Domen. $\endgroup$ Mar 13, 2023 at 16:50

Your Answer

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

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