I need to remove elements (that I am plotting with error bars, so I need to remove that data point from all four vectors below) where the diff vector is greater
diff
Don't use a for loop. Do something like this if you want to replace certain values by NA:
Z[diff >= 2*std] = NA
Alternatively, if you want to just filter out the rows that don't satisfy the condition, subset only the rows you want:
Z <- Z[diff < 2*std]