maxn: maxn

Description Usage Arguments Value Examples

View source: R/maxn.R

Description

A function to find the top nth-highest value in a vector From kohske on StackOverflow http://stackoverflow.com/questions/10296866/finding-the-column-number-and-value-the-of-second-highest-value-in-a-row/10297025#10297025

Usage

1
maxn(n)

Arguments

n

vector of numbers

Value

top nth value

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
mydata <- c(6, 10, 2, 21, 13, 20, 4, 12)
secondhighest <- mydata[maxn(2)(mydata)]

# or define function for second highest:
max2 <- maxn(2)
secondhighest <- mydata[max2(mydata)]

# To find second-highest number in each row in a dataframe:
## Not run: 
 apply(df, 1, function(x)x[maxn(2)(x)])

## End(Not run)

smach/rmiscutils documentation built on Dec. 15, 2021, 1:49 a.m.