Description Usage Arguments Value Author(s) Examples
Takes a matrix and computes the arithmetic mean of the top elemens for each single row.
1 | topN(x, nth)
|
x |
a numeric matrix |
nth |
an integer giving the desired number of top elemens |
A numeric vector containing the respective means of the top nth
elements in each row.
Dominik Mueller
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))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.