Description Usage Arguments Examples
This is a convenient wrapper that uses filter
and
min_rank
to select the top or bottom entries in each group,
ordered by wt
.
1 |
x |
a |
n |
number of rows to return. If If |
wt |
(Optional). The variable to use for ordering. If not specified, defaults to the last variable in the tbl. |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | df <- data.frame(x = c(10, 4, 1, 6, 3, 1, 1))
df %>% top_n(2)
# Negative values select bottom from group. Note that we get more
# than 2 values here because there's a tie: top_n() either takes
# all rows with a value, or none.
df %>% top_n(-2)
if (require("Lahman")) {
# Find 10 players with most games
# A little nicer with %>%
tbl_df(Batting) %>%
group_by(playerID) %>%
tally(G) %>%
top_n(10)
# Find year with most games for each player
tbl_df(Batting) %>% group_by(playerID) %>% top_n(1, G)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.