To expand my comment, I would do something like this:
for (j in names(tmp)) { col = tmp[[j]] min_2 = sort.int(unique(col), partial=2L)[2L] # 2nd lowest value set(tmp, i = which(col > min_2), j = j, value = 0L) }
This is done across all columns in tmp and gets the second minimum value for each column using sort.int with the partial argument, which is slightly more efficient than using sort (since we donβt have to sort the entire data set to find the second minimum value).
Then we use set() to replace those rows where the column value is greater than the second minimum value for this column with a value of 0.
source share