David David - 8 months ago 36
R Question

Finding the 2nd Max value in R data table

I have a data table that has a int column that is counting the iteration of a test that I've preformed. So the first time I preformed the test,

Table$Column == 1
and the second time
Table$Column == 2
and so on and so forth.

However, there are times when a test breaks or I don't want to use the results from a particular test run. I have code that compares the current run vs. the previous run that looks like this.


But since I sometimes omit results, it breaks vPrevious. How could I replace the "-1" with a function that finds the "2nd" max value?


This should work:

vPrevious <- sort(Table$Column, decreasing=T)[2]

We first sort in descending order and then pick the second element.