slice: Subset rows by position

View source: R/slice.R

sliceR Documentation

Subset rows by position

Description

Subset rows by their original position in the data.frame. Grouped data.frames 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 data.frame.

...

For slice(): integer row values.

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 n, the number of rows, or prop, the proportion of rows to select. If neither are supplied, n = 1 will be used.

If n is greater than the number of rows in the group (or prop > 1), the result will be silently truncated to the group size. If the proportion of a group size is not an integer, it is rounded down.

A negative value of n or prop gives the number or proportion of rows to drop from each group, e.g. n = -2 keeps all rows but the last two in slice_head(). Any fractional part of a negative n is ignored, and the number of rows a negative prop drops is rounded down. slice_sample() does not accept a negative n or prop.

n = 0 or prop = 0 selects no rows, as does a negative n or prop which drops at least as many rows as a group has. This differs from dplyr 1.0.10, where n = 0 and prop = 0 return every row in slice_head(), slice_tail(), slice_min() and slice_max() and are an error in slice_sample(), and where slice_min() and slice_max() with with_ties = FALSE can return rows when a negative n or prop is larger than the group. Later versions of dplyr select no rows, as poorman does.

order_by

A variable, or an expression of the variables, to order by, e.g. x * y. It must have a value for each row of the group.

with_ties

logical(1). Should ties be kept together? The default, TRUE, may return more rows than you request. Use FALSE to ignore ties, and return the first n rows.

weight_by

Sampling weights. This must evaluate to a vector of non-negative numbers the same length as the input, or the group on grouped data. Weights are automatically standardised to sum to 1.

replace

logical(1). Should sampling be performed with (TRUE) or without (FALSE, the default) replacement.

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)


poorman documentation built on Oct. 29, 2026, 1:07 a.m.