3
$\begingroup$

A simple example of what I'm trying to do (but I want to be able to apply the same principle to much larger data sets and with more complicated functions for low and high):

data = {1, 5, 6, 9, 13, 20, 67, 68, 80, 97}; 
xList = Range[10]; 
low = (#1^1.9 & ) /@ xList; 
high = (#1^2.1 & ) /@ xList; 
table = Transpose[Join[{xList, low, data, high}, 2]]; 
TableForm[table]

I want to apply a conditional Style to data:

If[low < data < high, Style[data, Green], Style[data, Red]]

Easy in principle - but everything I have tried results in error messages and strings of data that aren't in TableForm.

How do I do it?

UPDATE:

Many thanks to @Lucas Lang for leading me to

data = {1, 5, 6, 9, 13, 20, 67, 68, 80, 97};
xList = Range[10];
low = (#1^1.9 &) /@ xList;
high = (#1^2.1 &) /@ xList;
TableForm@
 MapThread[{#, #2, Style[#3, If[Less@##2, Green, Red]], #4} &, {xList,
 low, data, high}]

This works - but only until I add an extra column. I'd like to add a column that gives high-low. But when I do so, all the green items in the column data go red...

data = {1, 5, 6, 9, 13, 20, 67, 68, 80, 97};
xList = Range[10];
low = (#1^1.9 &) /@ xList;
high = (#1^2.1 &) /@ xList;
highMinusLow = high - low;
TableForm@
 MapThread[{#, #2, 
  Style[#3, If[Less@##2, Green, Red]], #4, #5} &, {xList, low, data,
  high, highMinusLow}]

Any solutions?

$\endgroup$
  • 1
    $\begingroup$ Just a small remark: Join[{xList, low, data, high}, 2] doesn't actually join anything. It just returns the list {xList, low, data, high}. Join works by supplying lists as separate arguments, not by supplying them all in a list in the first argument. However, Join[xList, low, data, high, 2] will only work for matrices or higher order arrays (joining their columns together). $\endgroup$ – Sjoerd Smit Sep 6 at 15:33
3
$\begingroup$

You can use MapThread to apply a function to each entry:

TableForm@MapThread[
  Style[#3, If[#2 < #3 < #4, Green, Red]] &,
  {xList, low, data, high}
  ]

Closer to your original code:

TableForm@MapThread[
  If[#2 < #3 < #4, Style[#3, Green], Style[#3, Red]] &,
  {xList, low, data, high}
  ]
(* same output *)

With the other columns present as well:

TableForm@MapThread[
  {#, #2, Style[#3, If[#2 < #3 < #4, Green, Red]], #4} &,
  {xList, low, data, high}
  ]

$\endgroup$
  • $\begingroup$ Hi @Lucas Lang. I'm very grateful for this - but could you please see the update to the OP? If I add another column, it all goes wrong... $\endgroup$ – Richard Burke-Ward Sep 6 at 14:24
  • $\begingroup$ Ooops, I might have gone to far with the conciseness... it should work now - the issue was that Less@##2 is equivalent to #2<#3<#4<... - which obviously breaks if you add more columns. Explicitly writing #2<#3<#4 fixes the issue $\endgroup$ – Lukas Lang Sep 6 at 14:29

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.