max_min_n: Maxima and Minima _n_ elements

Description Usage Arguments Details Value Examples

Description

Obtain the Maximum or Minimum n elements from a vector.

Usage

1
2
3
max_n(x, n = 1L)

min_n(x, n = 1)

Arguments

x

Data vector

n

Number of observations to select

Details

The underlying function sorts the data using base::sort() and then extracts out the appropriate n-back or n-forward values.

As a result of the sorting procedure, this is an inefficient function.

Value

A vector containing the maximum/minimum of n elements.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
x = 1:10

# Defaults to traditional max
# This is more costly to compute than using the regular max function.
max_n(x) 

# Retrieve top two observations (highest first)
max_n(x, 2)

# Missing values have no effect on the sorting procedure
x[9] = NA
max_n(x, 3)

# Defaults to traditional min.
# This is more costly to compute than using the regular min function.
min_n(x)
min(x)

# Retrieve bottom two observations (lowest first)
min_n(x, 2)

# Missing values have no effect on the sorting procedure
x[2] = NA
min_n(x, 3)

jjb documentation built on Jan. 8, 2020, 5:07 p.m.