Learn R Programming

tidyfst (version 0.8.8)

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

Description

Analogous function for top_n and top_frac in dplyr, but with a different API.

In top_dt, you can use an API for both functionalities in `top_n_dt()` and `top_frac_dt()`.

Usage

top_dt(data, wt = NULL, n = NULL, prop = NULL)

top_n_dt(data, n, wt = NULL)

top_frac_dt(data, prop, wt = NULL)

Arguments

data

data.frame

wt

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

n

Number of rows to return. Will include more rows if there are ties. If n is positive, selects the top rows. If negative, select the bottom rows.

prop

Fraction of rows to return. Will include more rows if there are ties. If prop is positive, selects the top rows. If negative, select the bottom rows.

Value

data.table

See Also

top_n

Examples

Run this code
# NOT RUN {
iris %>% top_n_dt(10,Sepal.Length)
iris %>% top_n_dt(-10,Sepal.Length)
iris %>% top_frac_dt(.1,Sepal.Length)
iris %>% top_frac_dt(-.1,Sepal.Length)

# For `top_dt`, you can use both modes above
iris %>% top_dt(Sepal.Length,n = 10)
iris %>% top_dt(Sepal.Length,prop = .1)
# }

Run the code above in your browser using DataLab