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)

Arguments

x

a tbl() to filter

n

number of rows to return. If x is grouped, this is the number of rows per group. Will include more than n rows if there are ties.

If n is positive, selects the top n rows. If negative, selects the bottom n rows.

wt

(Optional). The variable to use for ordering. If not specified, defaults to the last variable in the tbl.

This argument is automatically quoted and later evaluated in the context of the data frame. It supports unquoting. See vignette("programming") for an introduction to these concepts.

Examples

df <- data.frame(x = c(10, 4, 1, 6, 3, 1, 1)) df %>% top_n(2)
#> Selecting by x
#> x #> 1 10 #> 2 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)
#> Selecting by x
#> x #> 1 1 #> 2 1 #> 3 1
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) }
#> Selecting by n
#> # A tibble: 19,516 x 22 #> # Groups: playerID [18,659] #> playerID yearID stint teamID lgID G AB R H X2B X3B #> <chr> <int> <int> <fctr> <fctr> <int> <int> <int> <int> <int> <int> #> 1 abercda01 1871 1 TRO NA 1 4 0 0 0 0 #> 2 armstbo01 1871 1 FW1 NA 12 49 9 11 2 1 #> 3 barkeal01 1871 1 RC1 NA 1 4 0 1 0 0 #> 4 barrebi01 1871 1 FW1 NA 1 5 1 1 1 0 #> 5 barrofr01 1871 1 BS1 NA 18 86 13 13 2 1 #> 6 bassjo01 1871 1 CL1 NA 22 89 18 27 1 10 #> 7 bellast01 1871 1 TRO NA 29 128 26 32 3 3 #> 8 berkena01 1871 1 PH1 NA 1 4 0 0 0 0 #> 9 berryto01 1871 1 PH1 NA 1 4 0 1 0 0 #> 10 berthha01 1871 1 WS3 NA 17 73 17 17 1 1 #> # ... with 19,506 more rows, and 11 more variables: HR <int>, RBI <int>, #> # SB <int>, CS <int>, BB <int>, SO <int>, IBB <int>, HBP <int>, SH <int>, #> # SF <int>, GIDP <int>