# Replacing data in a new table using the index

This problem might be similar to others but I could not find the solution with the extra step I need.

I have two tables with the same value on the x axis, and different in the y. I want to pick values in y larger than 0.7 in the first column and replace those in the second table. In this example I am using random numbers, I just need to understand the mechanism

So far, I did this

table1={{0.271719, 0.753123}, {0.222337, 0.862412}, {0.953062,
0.102117}, {0.175489, 0.0383113}, {0.50512, 0.972545}, {0.0674241,
0.520495}, {0.356298, 0.665223}, {0.0345514, 0.368893}, {0.47518,
0.920533}, {0.886116, 0.818796}, {0.276576, 0.0776354}, {0.0954226,
0.396029}, {0.552711, 0.199267}, {0.580011, 0.117124}, {0.580966,
0.851715}, {0.231753, 0.721055}, {0.212908, 0.564966}, {0.626957,
0.378747}, {0.995028, 0.0074514}, {0.871566, 0.695403}, {0.889934,
0.205749}, {0.666136, 0.536372}, {0.579304, 0.365205}, {0.407595,
0.352883}, {0.508853, 0.470274}, {0.070248, 0.184634}, {0.470959,
0.638688}, {0.520654, 0.297228}, {0.770984, 0.54014}, {0.0850685,
0.853628}, {0.773723, 0.784571}, {0.617642, 0.389093}, {0.819015,
0.336571}, {0.771288, 0.763936}, {0.761571, 0.85472}, {0.726057,
0.863356}, {0.791428, 0.135676}, {0.564558, 0.528138}, {0.775503,
0.145516}, {0.211066, 0.263744}}

table2={{0.271719, 0.0289402}, {0.222337, 0.255539}, {0.953062,
0.037332}, {0.175489, 0.657431}, {0.50512, 0.266119}, {0.0674241,
0.940589}, {0.356298, 0.294566}, {0.0345514, 0.999265}, {0.47518,
0.408269}, {0.886116, 0.535559}, {0.276576, 0.443232}, {0.0954226,
0.639447}, {0.552711, 0.916857}, {0.580011, 0.459322}, {0.580966,
0.00344053}, {0.231753, 0.661201}, {0.212908, 0.75885}, {0.626957,
0.0856721}, {0.995028, 0.677893}, {0.871566, 0.519258}, {0.889934,
0.272003}, {0.666136, 0.323592}, {0.579304, 0.352075}, {0.407595,
0.260367}, {0.508853, 0.922565}, {0.070248, 0.8942}, {0.470959,
0.194508}, {0.520654, 0.929323}, {0.770984, 0.521443}, {0.0850685,
0.730803}, {0.773723, 0.906517}, {0.617642, 0.178376}, {0.819015,
0.628246}, {0.771288, 0.0813782}, {0.761571, 0.769628}, {0.726057,
0.233538}, {0.791428, 0.841783}, {0.564558, 0.240659}, {0.775503,
0.559572}, {0.211066, 0.689201}}


I want to select from table 1 all the values in the second column larger than 0.7. I do this,

Select[table1, #[[2]] > 0.7&]

1. At this point, I want to replace the selected elements in table 2. I am not sure how to proceed. I tried with PositionIndex to see what is the index but then I do not know how move forward.
• Try: pos = Position[table1, {, x} /; x > 0.7] // Flatten; (table2[[#, 2]] = table1[[#, 2]]) & /@ pos Nov 3, 2021 at 8:15
• How is this working? I tried but it gives empty lists Nov 3, 2021 at 8:18
• The underscores are missing: Position[table1, {_, x_} /; x > 0.7] // Flatten; (table2[[#, 2]] = table1[[#, 2]]) & /@ pos Nov 3, 2021 at 8:26
• @DanielHuber now it works but it eliminates the rest of the entries. I just want to replace the elements which are above 0.7 and keep the rest as they are in table 2 Nov 3, 2021 at 8:32
• Yes, it returns the replaced entries, but it also changes table2 where the y are replaced. Nov 3, 2021 at 8:56

1. Position + ReplacePart:

positions = Flatten @ Position[table1[[All, 2]] - .7, _?Positive];

tableA = ReplacePart[table1, Thread[positions -> table2[[positions]]]];

MapAt[Highlighted, #, Thread[{positions, 2}]], #2, Top] &,
{{table1, table2, tableA}, {"table1", "table2", "tableA"}}]


2. Position + Part assignment:

tableB = table1;
tableB[[positions]] = table2[[ positions]];

tableB == tableA

 True


3. MapIndexed:

tableC = MapIndexed[If[#[[2]] > .7, table2[[#2[[1]]]], #] &, table1, {1}];

tableC == tableA

 True


mask = Boole[Positive[table1[[All, 2]] - .7]];
tableD = table1 + mask (table2 - table1);

tableD == tableA

 True


I will go with my best interpretation of the OP's reqs. Verified that the following returns "True" as asserted in the post:

table1[[All, 1]] == table2[[All, 1]]


Collect the first col (same for both) and then the second column from each table.

dlist = Transpose@{table1[[All, 1]], table1[[All, 2]],
table2[[All, 2]]};


For each entry if b>0.7, then replace c with b.

elist = dlist /. {a_, b_, c_} /; b > 0.7 -> {a, b, b}


Extract the first and third columns.

theList = {#[[1]], #[[3]]} & /@ elist


If my interpretation is wrong, then please do indicate.

• Thanks! This is very useful, can this be done using the positions instead? I ask because I then have another table with x (time) and y (energy) and I want to replace in this table the values of another table corresponding to the position for which the b>0.7 is true Nov 3, 2021 at 14:05
• It would be a good idea to post it as a separate question along with sufficient data. The above solution depends on the fact that the first columns of both data tables are identical. It is easy to find positions in a table where a value exceeds threshold, but then what these positions to be filled with? data from similar positions in another table? or data having the same x magnitude in another table? both of these values may or may not readily exist.
– Syed
Nov 3, 2021 at 15:21