top_n | R Documentation |
Select the top or bottom entries in each group, ordered by wt
.
top_n(.df, n = 5, wt = NULL, .by = NULL)
.df |
A data.frame or data.table |
n |
Number of rows to return |
wt |
Optional. The variable to use for ordering. If NULL uses the last column in the data.table. |
.by |
Columns to group by |
df <- data.table(
x = 1:5,
y = 6:10,
z = c(rep("a", 3), rep("b", 2))
)
df %>%
top_n(2, wt = y)
df %>%
top_n(2, wt = y, .by = z)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.