Last active
October 3, 2019 15:50
-
-
Save randrescastaneda/2ba31f1f3f2156c7ed0d27ea0331316e to your computer and use it in GitHub Desktop.
N highest of vector. This function was created by [Zach](https://stackoverflow.com/users/345660/zach) in this [post](https://stackoverflow.com/questions/2453326/fastest-way-to-find-second-third-highest-lowest-value-in-vector-or-column). I just keep it here for storage purposes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
maxN <- function(x, N=2, decreasing = FALSE){ | |
len <- length(x) | |
if(N>len){ | |
warning('N greater than length(x). Setting N=length(x)') | |
N <- length(x) | |
} | |
if (decreasing == FALSE) { | |
sort(x,partial=len-N+1)[len-N+1] | |
} else { | |
-sort(-x,partial=len-N+1)[len-N+1] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment