top_n {dplyr} | R Documentation |
This is a convenient wrapper that uses filter
and
min_rank
to select the top n entries in each group, ordered
by wt
.
top_n(x, n, wt)
x |
a |
n |
number of rows to return. If |
wt |
(Optional). The variable to use for ordering. If not specified, defaults to the last variable in the tbl. |
# Find 10 players with most games if (require("Lahman")) { players <- group_by(tbl_df(Batting), playerID) games <- tally(players, G) top_n(games, 10, n) # 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) }