slice.dtplyr_step {dtplyr} | R Documentation |
These are methods for the dplyr slice()
, slice_head()
, slice_tail()
,
slice_min()
, slice_max()
and slice_sample()
generics. slice()
and slice_sample()
are translated to the i
argument of [.data.table
,
all others are translated to the j
argument.
Unlike dplyr, slice()
(and slice()
alone) returns the same number of
rows per group, regardless of whether or not the indices appear in each
group.
## S3 method for class 'dtplyr_step' slice(.data, ...) ## S3 method for class 'dtplyr_step' slice_head(.data, ..., n, prop) ## S3 method for class 'dtplyr_step' slice_tail(.data, ..., n, prop) ## S3 method for class 'dtplyr_step' slice_min(.data, order_by, ..., n, prop, with_ties = TRUE) ## S3 method for class 'dtplyr_step' slice_max(.data, order_by, ..., n, prop, with_ties = TRUE)
.data |
A |
... |
Positive integers giving rows to select, or negative integers giving rows to drop. |
n, prop |
Provide either If |
order_by |
Variable or function of variables to order by. |
with_ties |
Should ties be kept together? The default, |
library(dplyr, warn.conflicts = FALSE) dt <- lazy_dt(mtcars) dt %>% slice(1, 5, 10) dt %>% slice(-(1:4)) # First and last rows based on existing order dt %>% slice_head(n = 5) dt %>% slice_tail(n = 5) # Rows with minimum and maximum values of a variable dt %>% slice_min(mpg, n = 5) dt %>% slice_max(mpg, n = 5) # slice_min() and slice_max() may return more rows than requested # in the presence of ties. Use with_ties = FALSE to suppress dt %>% slice_min(cyl, n = 1) dt %>% slice_min(cyl, n = 1, with_ties = FALSE) # slice_sample() allows you to random select with or without replacement dt %>% slice_sample(n = 5) dt %>% slice_sample(n = 5, replace = TRUE) # you can optionally weight by a variable - this code weights by the # physical weight of the cars, so heavy cars are more likely to get # selected dt %>% slice_sample(weight_by = wt, n = 5)