topN: Average value of the top elements in each row of a matrix.

Description Usage Arguments Value Author(s) Examples

Description

Takes a matrix and computes the arithmetic mean of the top elemens for each single row.

Usage

1
topN(x, nth)

Arguments

x

a numeric matrix

nth

an integer giving the desired number of top elemens

Value

A numeric vector containing the respective means of the top nth elements in each row.

Author(s)

Dominik Mueller

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
(x <- apply(matrix(seq_len(16), ncol = 4), 2L, sample))
topN(x, 2L)

# benchmarking with a pure R solution
x <- matrix(rnorm(1000L ^ 2L), ncol = 1000L)
microbenchmark::microbenchmark(times = 1L,
  a <- apply(x, 1L, function(x) mean(head(sort(x, decreasing = TRUE), 20L))),
  b <- topN(x, 20L)
)
all(dplyr::near(a, b))

DominikMueller64/dmisc documentation built on May 6, 2019, 2:52 p.m.