Skip to main content
Tweeted twitter.com/StackMma/status/1170170229135171585
Update
Source Link

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?

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?

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?

Source Link

Conditional formatting of `Table` with `Join` and `Transpose`

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?