This is a convenient wrapper that uses filter()
and
min_rank()
to select the top or bottom entries in each group,
ordered by wt
.
top_n(x, n, wt) top_frac(x, n, wt)
x | a |
---|---|
n | number of rows to return for If If |
wt | (Optional). The variable to use for ordering. If not specified, defaults to the last variable in the tbl. |
Both n
and wt
are automatically quoted and later
evaluated in the context of the data
frame. It supports unquoting.
#>#> x #> 1 10 #> 2 6#>#> x #> 1 10 #> 2 4 #> 3 6df %>% top_frac(.5)#>#> x #> 1 10 #> 2 4 #> 3 6# 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)#>#> x #> 1 1 #> 2 1 #> 3 1if (require("Lahman")) { # Find 10 players with most games tbl_df(Batting) %>% group_by(playerID) %>% tally(G) %>% top_n(10) # Find year with most games for each player if (FALSE) { tbl_df(Batting) %>% group_by(playerID) %>% top_n(1, G) } }#>