keep_top_n: Filter a dataframe to keep the top n of a given column.

Description Usage Arguments Details Examples

Description

keep_top_n takes a data frame and filters it to only return the rows coinciding with the top n of a given column. This enables the user to quickly extract data based on the first or largest of something.

Usage

1
keep_top_n(.data, .ranking_col, n = 1)

Arguments

.data

A data.frame to apply the filter method too

.ranking_col

The column used to filter the data

n

The number of rows to keep

Details

The ranking mechanism uses the dplyr::row_number. This means if there are any ties when ranking the data, the data corresponding to the first row positionally is kept.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
library(dplyr)

data("starwars")

starwars %>%
  group_by(gender) %>%
  keep_top_n(height)

starwars %>%
  group_by(gender) %>%
  keep_top_n(desc(height), n = 3)

louis-vines/tidyutils documentation built on May 21, 2019, 12:05 p.m.