Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am looking for a method to highlight certain cells in a table/Grid based on meeting some user defined conditions.

As an example:

M = {{-0.6, 0.04, 22}, {0.1, 0.3, 1}, {0.4, 0.05,0.01}}; (*can be much larger *)

As purely an example, the data could be formatted as:

 TableForm[M, TableHeadings -> {{"x", "y", "z"}, {“correlation", "pValue", 
“tStatistic"}}]  (*number of rows and columns can be very different *)

sample data

I’d like to automatically highlight data which meets a certain user defined criteria say:

Abs@Correlation > 0.5 && Abs@pValue ≤ 0.05 

So the highlighted table would look like:

enter image description here

Ideally, I’d like a function that I can apply to a Table/Grid….

share|improve this question
    
Where do you want to use it, TableForm has headings but in Grid background can by applied via Options. –  Kuba Apr 15 at 20:18
    
Definitely use Grid, it's the more modern version for formatting tables. And it's backward compatible in the sense that the output of Grid in a text terminal is still formatted the same way as TableForm. The converse is also true: TableForm output in a notebook is internally formatted using GridBox. –  Jens Apr 15 at 20:32
    
Grid is my preferred method… old habits die hard… that I showed Tableform in my question –  Pam Apr 15 at 20:43

2 Answers 2

up vote 2 down vote accepted

There are likely many ways to go about this, but here is one which uses pattens and replacement rules:

data = {{-0.6, 0.04, 22}, {0.1, 0.3, 1}, {0.4, 0.05,0.01}};

rule = {a_, b_, c_} /; 
   Abs[a] > 0.5 && Abs[b] <= 0.05 :> 
  {Item[Style[a, Red, Bold], Background -> LightBlue, FrameStyle -> Darker[Red, 0.25], 
    Frame -> {{True, False}, {True, True}}], 
   Item[Style[b, Red, Bold], Background -> LightBlue, FrameStyle -> Darker[Red, 0.25], 
    Frame -> {{False, True}, {True, True}}], c} 

TableForm[data, 
  TableHeadings -> {{"x", "y", "z"}, {"correlation", "pValue", "tStatistic"}}] /. rule

enter image description here

You can also apply this rule to a grid as well:

Grid[data] /. rule

enter image description here

share|improve this answer
    
I didn’t realize that Grid has a direct way of handling these cases... –  Pam Apr 15 at 20:45
    
@Pam it has nothing to do with Grid :) it's a replacement rule fitted to your array. –  Kuba Apr 15 at 21:02
    
@chuy I'm not quite convinced that the frame will fit well for general case. –  Kuba Apr 15 at 21:03
    
@ Kuba Got it after I started to implement it… :-) –  Pam Apr 15 at 21:07
    
@Kuba I am quite sure it won't, (for example if the cells you want to highlight in this manner are not connected) but this should show how one could use Item and the options it takes to style as wanted. Personally I wouldn't use a frame and rely on background. –  chuy Apr 15 at 21:08

You can do something like:

TableForm[
 Map[If[Abs[#[[1]]] > 0.5 && #[[2]] <= 0.05, {Item[#[[1]], 
      Background -> LightBlue], Item[#[[2]], Background -> LightBlue], #[[3]]}, #] &, M], 
 TableHeadings -> {{"x", "y", "z"}, {"correlation", "pValue", "tStatistic"}}]

enter image description here

Grid has, as Kuba noted, a direct way of doing this, check the documentation.

If you want to do this on an existing tableform by naming columns and conditions, you'll need to parse the fullform of it, generate a list of column name to list entry mappings, and "inject" the changes. Not sure if that's worth the effort vs just manipulating the base data.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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