0
$\begingroup$

I am making a table of data that I wish to output using TeXForm. Rather than manipulate the values in LaTeX (e.g. using the siunitx package), I would like to have the numbers trimmed in MAA before export.

The dataset has the following form:

vallist = {697123., 1234.25, 530.25, 67.9101, 1.002679, 0.02570, 0.0000135, 0};

errlist = {1234.4, 210.16, 330.1, 0.0231, 0.0543, 0.000617, 0.0000026, 0.0019}

In order to make the final table neat/compact I would like to limit the output size of each value as follows:

  1. Values >= 100 presented as 4-significant figure integers (no decimal point); corrsponding error: 2-significant digits followed by zeros
  2. 100 > value >0.001: four significant digits; error: 2 significant digits limited to a maximum of 3 decimal places
  3. value <0.001: "<0.001" ; error: ""
  4. values of 0 or 0. -> "n.d." ; error: ""

So for the above lists the output would be something like:

{{697100, 1234, 530, 67.91, 1.003, 0.026, "<0.001", "n.d."}, 
 {2100,   210,  330, 0.023, 0.054, 0.001,   "",      ""}}

To format the values I've tried a set of nested If statements:

If[IntegerPart[#] \[GreaterSlantEqual] 100, IntegerPart[#], 
   If[# == 0, "n.d.", 
    If[Round[#, 0.001] == 0, "<0.001", Round[#, 0.001]]]](*]*)& /@ 
 SetPrecision[vallist, 4]

which gives:

{697123, 1234, 530, 67.91, 1.003, 0.026, "<0.001", "n.d."}

Which is not quite right (i.e. the largest value retains digits below the number of digits specified by SetPrecision.

I'm also not yet sure how to approach modifying the corresponding error values.

Can anyone suggest a (more) sensible way to format these numbers?

$\endgroup$
1
$\begingroup$

Using inspiration from this question about significant figures:

sf[x_, n_] := Module[{t}, 
  If[IntegerQ[t = Round[x, 10^(Floor[Log10[x]] - n + 1)]], t, N[t]]]

numform[{v_, e_}] := Which[
  v >= 100,
  {Round[sf[v, 4]], sf[e, 2]},
  v >= 0.001,
  {Round[sf[v, 4], 0.001], Round[sf[e, 2], 0.001]},
  v == 0,
  {"n.d.", ""},
  v < 0.001,
  {"<0.001", ""}
  ]

valerrlist = Transpose[{vallist, errlist}];
numform[#] & /@ valerrlist

{{697100, 1200}, {1234, 210}, {530, 330}, {67.91, 0.023}, {1.003, 0.054}, {0.026, 0.001}, {"<0.001", ""}, {"n.d.", ""}}

$\endgroup$
0
$\begingroup$

The following works for the dataset provided. Transpose the lists:

valerrlist = Transpose@{vallist, errlist}

{{697123., 1234.3}, {1234.25, 210.16}, {530.25, 330.1}, {67.9101, 0.0231}, {1.00268, 0.0543}, {0.0257, 0.000617}, {0.0000135, 2.6*10^-6}, {0, 0.0019}}

Define a function which looks at each number pair and formats the error values depending on the trimmed value.

format[{val_, err_}] := 
 Module[{valout, errout}, 
  valout = Which[IntegerPart[val] \[GreaterSlantEqual] 100, 
    IntegerPart[val], val == 0, "n.d.", Round[val, 0.001] == 0, 
    "<0.001", True, Round[val, 0.001]]; 
  errout = If[NumberQ[valout], 
    Which[IntegerPart[err] \[GreaterSlantEqual] 100, IntegerPart[err],
      err == 0, "", err < 0.0005, "", True, Round[err, 0.001]], ""];

  {valout, errout}
  ]
format[#] & /@ valerrlist

{{697123, 1234}, {1234, 210}, {530, 330},

{67.91, 0.023}, {1.003, 0.054}, {0.026, 0.001},

{"<0.001", ""}, {"n.d.", ""}}

I haven't tested it extensively but it seems to work ok.

%//TeXForm

\begin{array}{cc} 697100 & 1200 \\ 1230 & 210 \\ 530 & 330 \\ 67.91 & 0.023 \\ 1.003 & 0.054 \\ 0.026 & 0.001 \\ \text{$<$0.001} & \text{} \\ \text{n.d.} & \text{} \\ \end{array}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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