top_n: Select top (or bottom) n rows (by value)

Description Usage Arguments Examples

View source: R/top-n.R

Description

This is a convenient wrapper that uses filter() and min_rank() to select the top or bottom entries in each group, ordered by wt.

Usage

1
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

 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)
}

Example output

Attaching package: 'dplyr'

The following objects are masked from 'package:stats':

    filter, lag

The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union

Selecting by x
   x
1 10
2  6
Selecting by x
  x
1 1
2 1
3 1
Loading required package: Lahman
Selecting by n
Source: local data frame [19,261 x 22]
Groups: playerID [18,405]

# A tibble: 19,261 x 22
    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,251 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>

dplyr documentation built on Oct. 16, 2018, 5:04 p.m.