Fastest way to find second (third…) highest/lowest value in vector or column

Use the partial argument of sort(). For the second highest value:

n <- length(x)
sort(x,partial=n-1)[n-1]

Leave a Comment