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

top_n_dt(data, n, wt = NULL)

top_frac_dt(data, n, wt = NULL)

Arguments

data

data.frame

n

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

wt

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

Value

data.table

See also

Examples

iris %>% top_n_dt(10,Sepal.Length)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1: 7.6 3.0 6.6 2.1 virginica #> 2: 7.3 2.9 6.3 1.8 virginica #> 3: 7.2 3.6 6.1 2.5 virginica #> 4: 7.7 3.8 6.7 2.2 virginica #> 5: 7.7 2.6 6.9 2.3 virginica #> 6: 7.7 2.8 6.7 2.0 virginica #> 7: 7.2 3.2 6.0 1.8 virginica #> 8: 7.2 3.0 5.8 1.6 virginica #> 9: 7.4 2.8 6.1 1.9 virginica #> 10: 7.9 3.8 6.4 2.0 virginica #> 11: 7.7 3.0 6.1 2.3 virginica
iris %>% top_n_dt(-10,Sepal.Length)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1: 4.7 3.2 1.3 0.2 setosa #> 2: 4.6 3.1 1.5 0.2 setosa #> 3: 4.6 3.4 1.4 0.3 setosa #> 4: 4.4 2.9 1.4 0.2 setosa #> 5: 4.3 3.0 1.1 0.1 setosa #> 6: 4.6 3.6 1.0 0.2 setosa #> 7: 4.7 3.2 1.6 0.2 setosa #> 8: 4.4 3.0 1.3 0.2 setosa #> 9: 4.5 2.3 1.3 0.3 setosa #> 10: 4.4 3.2 1.3 0.2 setosa #> 11: 4.6 3.2 1.4 0.2 setosa
iris %>% top_frac_dt(.1,Sepal.Length)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1: 7.0 3.2 4.7 1.4 versicolor #> 2: 6.9 3.1 4.9 1.5 versicolor #> 3: 7.1 3.0 5.9 2.1 virginica #> 4: 7.6 3.0 6.6 2.1 virginica #> 5: 7.3 2.9 6.3 1.8 virginica #> 6: 7.2 3.6 6.1 2.5 virginica #> 7: 7.7 3.8 6.7 2.2 virginica #> 8: 7.7 2.6 6.9 2.3 virginica #> 9: 6.9 3.2 5.7 2.3 virginica #> 10: 7.7 2.8 6.7 2.0 virginica #> 11: 7.2 3.2 6.0 1.8 virginica #> 12: 7.2 3.0 5.8 1.6 virginica #> 13: 7.4 2.8 6.1 1.9 virginica #> 14: 7.9 3.8 6.4 2.0 virginica #> 15: 7.7 3.0 6.1 2.3 virginica #> 16: 6.9 3.1 5.4 2.1 virginica #> 17: 6.9 3.1 5.1 2.3 virginica
iris %>% top_frac_dt(-.1,Sepal.Length)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species #> 1: 4.7 3.2 1.3 0.2 setosa #> 2: 4.6 3.1 1.5 0.2 setosa #> 3: 4.6 3.4 1.4 0.3 setosa #> 4: 4.4 2.9 1.4 0.2 setosa #> 5: 4.8 3.4 1.6 0.2 setosa #> 6: 4.8 3.0 1.4 0.1 setosa #> 7: 4.3 3.0 1.1 0.1 setosa #> 8: 4.6 3.6 1.0 0.2 setosa #> 9: 4.8 3.4 1.9 0.2 setosa #> 10: 4.7 3.2 1.6 0.2 setosa #> 11: 4.8 3.1 1.6 0.2 setosa #> 12: 4.4 3.0 1.3 0.2 setosa #> 13: 4.5 2.3 1.3 0.3 setosa #> 14: 4.4 3.2 1.3 0.2 setosa #> 15: 4.8 3.0 1.4 0.3 setosa #> 16: 4.6 3.2 1.4 0.2 setosa