# Condition on the data of table

If you have a table and want to separate some data into columns according to certain conditions:

Let's assume we have the following table:

fr = Subscript[x, 1] Subscript[x, 2] +
Subscript[x, 1] Subscript[x, 3] -
Subscript[x, 1] Subscript[x, 2] Subscript[x, 3] ;
(data = Tuples[{0, 1}, 3]) // MatrixForm; LLR1 = {Block[{Subscript},
Do[Subscript[x, j] = Transpose[data][[j]], {j, 3}];
fr]}/(Binomial[2, {data[[All, 1]] + data[[All, 2]]}] Binomial[
1, {data[[All, 3]]}]);
(table = Prepend[
Transpose@
Join[{Range@Length@data},
Transpose@
data, {data[[All, 1]] + data[[All, 2]]}, {data[[All, 3]]},
LLR1], Flatten[{"order", Subscript["x", #] & /@ Range[3], "l1",
"l2", "f", "h0", "h1", "h2"}]]) // Grid[#, Frame -> All] &


We set the condition to form column h2:

(If $$x_1x_2=1$$ take the value of f)

We set the condition to form column h1:

(h1=f-h2)

We set the condition to form column h0:

(h0 is the complement of f)

The result should be as follows

Thanks for the help

You can use ReplaceAll to replace each row (omitting the header row). By using patterns instead of indices, you minimize the risk of mismatching the values.

table /. {data :
PatternSequence[order_?NumericQ, x1_, x2_, x3_, l1_, l2_, f_], ___} :>
With[{h2 = If[x1 x2 == 1, f, 0]}, {data, 1 - f, f - h2, h2}] //
Grid[#, Frame -> All] &


Note that ?NumericQ is there to assure you do not modify the first (header) row.

• Is this function available in the version 11 Commented Aug 2 at 16:42
• @Emadkareem, it's not. I've changed my answer to using ReplaceAll, which is available in v11. Commented Aug 2 at 16:48
• If I have two conditions apply to the data. Let us assume h2=If[x1 x2 == 1, f, 0] and h3=If[x1 x3 == 1, f, 0]. I wrote it in the form With[{h2 = If[x1 x2 == 1, f, 0]},{h3 = If[x1 x3 == 1, f, 0]}, {data, 1 - f, f - (h2+h3), h2,h3}. But the function With does not work with more than one condition. Commented Aug 7 at 8:11
• You can replace With with Module, and give definitions inside: Module[{h2, h3}, h2 = If[x1 x2 == 1, f, 0]; h3 = If[x1 x3 == 1, f, 0]; {data, 1 - f, f - (h2+h3), h2,h3}] Commented Aug 7 at 13:20
• I have used it this way With[{h2 = If[x1 x2 == 1, f, 0],h3 = If[x1 x3 == 1, f, 0]}, {data, 1 - f, f - (h2+h3), h2,h3} and it gave accurate results...are there any risks involved? Commented Aug 7 at 16:51

It is easier to work with the columns of the table as a list and eliminate the header:

tmp = Transpose[(Join[#, {0, 0, 0}]) & /@ table[[2 ;;]]];


Next we insert the missing columns:

tmp = Join[Transpose[table[[2 ;;]]], ConstantArray[0, {3, 8}]];


Now we apply the conditions:

tmp[[10]] = tmp[[7]] ((If[# == 1, 1, #]) & /@ (tmp[[2]]  tmp[[3]]));
tmp[[9]] = tmp[[7]] - tmp[[10]];
tmp[[8]] = (Switch[#, 1, 0, 0, 1, _, #]) & /@ tmp[[7]];


Finally we transpose tmp to get the original table back and reinsert the headings:

tmp = Prepend[Transpose[tmp], table[[1, All]]];


Now we can display the change table:

tmp // Grid[#, Frame -> All] &


• Can I generalize this answer if the number of columns in the table is more than 6? Commented Aug 2 at 16:36
• Sure, "table[[2 ;;]]" will get all the columns until the end. And you will need to adapt the numbers in the code. Commented Aug 2 at 16:58