top_n_row | R Documentation |
One often wants to know which are the largest n elements in each of the rows or columns of a matrix. These functions extract the indices of these elements (using naive ranking).
top_n_row(m, n) top_n_col(m, n)
m |
matrix |
n |
number of elements to extract. Unlike dplyr's
|
a two-column subscript matrix with row indices in the first
column and column indices in the second. This can be used as a single
subscript to the input matrix m
to yield a vector.
m <- matrix(1:9, ncol=3) ij_row <- top_n_row(m, 2) ij_col <- top_n_col(m, 2) # note the resulting grouping by rows/cols m[ij_row] m[ij_col] data.frame(rownum=ij_row[ , 1], value=m[ij_row])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.