Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

slice..html

Choose rows in a data.table


Description

Choose rows in a data.table. Grouped data.tables grab rows within each group.

Usage

slice.(.df, ..., .by = NULL)

slice_head.(.df, n = 5, .by = NULL)

slice_tail.(.df, n = 5, .by = NULL)

slice_max.(.df, order_by, n = 1, .by = NULL)

slice_min.(.df, order_by, n = 1, .by = NULL)

slice_sample.(.df, n, prop, weight_by = NULL, replace = FALSE, .by = NULL)

Arguments

.df

A data.frame or data.table

...

Integer row values

.by

Columns to group by

n

Number of rows to grab

order_by

Variable to arrange by

prop

The proportion of rows to select

weight_by

Sampling weights

replace

Should sampling be performed with (TRUE) or without (FALSE, default) replacement

Examples

test_df <- data.table(
  x = 1:4,
  y = 5:8,
  z = c("a","a","a","b")
)

test_df %>%
  slice.(1:3)

test_df %>%
  slice.(1, 3)

test_df %>%
  slice.(1:2, .by = z)

test_df %>%
  slice_head.(1, .by = z)

test_df %>%
  slice_tail.(1, .by = z)

test_df %>%
  slice_max.(order_by = x, .by = z)

test_df %>%
  slice_min.(order_by = y, .by = z)

tidytable

Tidy Interface to 'data.table'

v0.6.1
MIT + file LICENSE
Authors
Mark Fairbanks [aut, cre], Abdessabour Moutik [ctb], Matt Carlson [ctb], Ivan Leung [ctb], Ross Kennedy [ctb]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.