Fastest way that I can think of to specifically find the second highest value from a vector:
## R CODE:
> ## create vector
> vector_data <- c(1, 4, 5, 6, 9)
> is.vector(vector_data)
[1] TRUE
> ## get second highest ranking number in the vector
> new_vector <- vector_data[! vector_data %in% max(vector_data)]
> new_vector
[1] 1 4 5 6
> max(new_vector)
[1] 6
You can replace max() with min() if you'd like to go the opposite way.
Other way that I would prefer:
- Convert the vector to a column in a dataframe. Sort the dataframe in ascending or descending order and reset the index. Create a rank column that mirrors that index. Now you have a rank column from which you can subset.
But like you said, you can also sort the vector in descending or ascending order, then select by position.