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?
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$