# Using If condition to replace columns of a matrix

I have two matrices:

tableone = {{1, "M", "0"}, {2, "M", 0}, {3, "F", 0}, {4, "M", 0}, {5, "F", 0}, {6, "M", 0}, {7, "F", 0}, {8, "F", 0}, {9, "M", 0}, {10, "F", 0}}

and

tabletwo = {{1, 10}, {2, 20}, {1, 20}, {4, 30}, {1, 30}, {5, 10}, {2, 10}, {3, 10}, {1, 50}}

If the expected value of the third column of table 2 is greater than 20, change all values 0 in the third column in tableone to the value 1.

Indeed,

totalcolumnsum = Sum[tabletwo[[i, 2]], {i, 1, 9}]

I've tried

Meantransaction = N[totalcolumnsum/Dimensions[tabletwo][[1]]]

If[Meantransaction > 20, ReplacePart[tableone[[;; , 3]] -> 1, tableone]] is greater than 20.

which I believe is incomplete and possibly in the wrong direction.

Can someone provide some help?

• What is Meantransaction? I suggest that you name MMA variables in lowercase... – José Antonio Díaz Navas Feb 2 '18 at 12:09
• @JoséAntonioDíazNavas I'll be more careful in the future. I've edited the OP. Meantransaction is the expected value of the second column in tabletwo. – Physkid Feb 2 '18 at 12:15
• @Physkid tabletwo does not have a third column ,futhermore tabletwo and tableone have not the same length – Alucard Feb 2 '18 at 14:57

Try with this:

ReplacePart[tableone, {#, 3} -> 1 & /@
Flatten@Position[tabletwo, {x_, y_} /; y > N@(Total[tabletwo][[2]]/Dimensions[tabletwo][[1]])]]

(*
{{1, "M", 0},
{2, "M", 0},
{3, "F", 0},
{4, "M", 1},
{5, "F", 1},
{6, "M", 0},
{7, "F", 0},
{8, "F", 0},
{9, "M", 1},
{10, "F",0}}
*)