Learn R Programming

Tmisc (version 0.1.22)

keep_top_n: Keep rows from top groups of a column

Description

Extract rows belonging to top n groups of a certain column

Usage

keep_top_n(.data, col, n = 10)

Arguments

.data

The data frame to operate on

col

A formula indicating the column to group over

n

The number of top groups to extract

Value

A data frame conaining only rows belonging to the top n groups of the column

Examples

Run this code
# NOT RUN {
# All the cars
(nrow(mtcars))
# Only those in the top 2 groups based on the # carbs
nrow(keep_top_n(mtcars, ~carb, n=2))
# }

Run the code above in your browser using DataLab