# Replace the values of the second position in a list given two conditions

Suppose I have the following list:

https://pastebin.com/zFin7kkB

and I have a given value of maxDh (let's say 0.5) . How can I replace only the values in the second position with a given operation (e.g. maxDh*5) only when the values of the first position are greater than 74 and maxDh is lower than 1.2 (as in this case)?. For example given that in this example maxDh=0.5, then I would get: {{40,0.0712996},{40.,0.0712996},{40.,0.0712996}......{74.0202,2.5},{74.0404,2.5},{74.2023,2.5}....etc.

Thank you very much in advanced,

• Perhaps list /. {a_?(# > 74 &), b_?(# < 1.2 &)} :> {a, 2.5} ? Can you explain what dhMax is here though? Is it the second element of each pair? May 24 '20 at 0:52
• Looks good, comparing ListPlot[%] before & after May 24 '20 at 0:59
• @MarcoB Thank you!. In this example maxDh (which I corrected in the EDIT) is the number that tells me if the condition should be done or not. Anytime that maxDh is lower than 1.2, then I need to replace all of the values in the second position with the given operation only when the values of the first postion are greater than 74. So, In the case of your code perhaps it would be something like: list /. {a_?(# > 74 &), b_?(maxDh < 1.2 &)} :> {a, 2.5}? but it does not seem to work like this
– John
May 24 '20 at 1:04
• Also, it is worth mentioning that maxDh has nothing to do with the list. It's just a number I use in my code that tells me if I should apply the requested code or not. If it is below 1.2 then I applied the requested code with the condition, if It is greater than 1.2, then simply the list remains the same and nothing happens.
– John
May 24 '20 at 1:10

Is this what you need?

Clear[list]
list = ToExpression@Import["https://pastebin.com/raw/zFin7kkB"];

ClearAll[conditionalreplace]
conditionalreplace[list_List, maxDh_: 0.5, threshold_: 1.2] :=
If[
maxDh < threshold,
list /. {a_?(# > 74 &), b_} :> {a, 5 maxDh},
list
]

ListLinePlot[{list, conditionalreplace[list]}]


• yes! Thank you very much !
– John
May 24 '20 at 1:48
• MarcoB, I have posted a similar question but with some modification here: mathematica.stackexchange.com/questions/222505/… . Perhaps you can help me there too as you are already familiar with the problem. Thanks !
– John
May 24 '20 at 2:29