slice {poorman} | R Documentation |
Subset rows by position
Description
Subset rows by their original position in the data.frame
. Grouped data.frame
s use the position within each group.
Usage
slice(.data, ...)
slice_head(.data, ..., n, prop)
slice_tail(.data, ..., n, prop)
slice_min(.data, order_by, ..., n, prop, with_ties = TRUE)
slice_max(.data, order_by, ..., n, prop, with_ties = TRUE)
slice_sample(.data, ..., n, prop, weight_by = NULL, replace = FALSE)
Arguments
.data |
A |
... |
For Provide either positive values to keep, or negative values to drop. The values provided must be either all positive or negative. Indices beyond the number of rows in the input are silently ignored. |
n , prop |
Provide either If |
order_by |
The variable to order by. |
with_ties |
|
weight_by |
Sampling weights. This must evaluate to a vector of non-negative numbers the same length as the input. Weights are automatically standardised to sum to 1. |
replace |
|
Value
An object of the same type as .data
. The output has the following properties:
Each row may appear 0, 1, or many times in the output.
Columns are not modified.
Groups are not modified.
Data frame attributes are preserved.
Examples
slice(mtcars, c(1, 2, 3))
mtcars %>% slice(1:3)
# Similar to head(mtcars, 1)
mtcars %>% slice(1L)
# Similar to tail(mtcars, 1):
mtcars %>% slice(n())
mtcars %>% slice(5:n())
# Rows can be dropped with negative indices:
slice(mtcars, -(1:4))
# First and last rows based on existing order
mtcars %>% slice_head(n = 5)
mtcars %>% slice_tail(n = 5)
# Grouped operations:
mtcars %>% group_by(am, cyl, gear) %>% slice_head(n = 2)