slice: Subset rows using their positions

sliceR Documentation

Subset rows using their positions

Description

slice() lets you index rows by their (integer) locations. It allows you to select, remove, and duplicate rows. It is accompanied by a number of helpers for common use cases:

  • slice_head() and slice_tail() select the first or last rows.

  • slice_sample() randomly selects rows.

  • slice_min() and slice_max() select rows with the smallest or largest values of a variable.

If .data is a grouped_df, the operation will be performed on each group, so that (e.g.) slice_head(df, n = 5) will select the first five rows in each group.

Usage

## S3 method for class 'SummarizedExperiment'
slice(.data, ..., .preserve = FALSE)

Arguments

.data

A data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr). See Methods, below, for more details.

...

For slice(): <data-masking> Integer row values.

Provide either positive values to keep, or negative values to drop. The values provided must be either all positive or all negative. Indices beyond the number of rows in the input are silently ignored.

For ⁠slice_*()⁠, these arguments are passed on to methods.

.preserve

Relevant when the .data input is grouped. If .preserve = FALSE (the default), the grouping structure is recalculated based on the resulting data, otherwise the grouping is kept as is.

Details

Slice does not work with relational databases because they have no intrinsic notion of row order. If you want to perform the equivalent operation, use filter() and row_number().

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.

Methods

These function are generics, which means that packages can provide implementations (methods) for other classes. See the documentation of individual methods for extra arguments and differences in behaviour.

Methods available in currently loaded packages:

  • slice(): \Sexpr[stage=render,results=rd]{dplyr:::methods_rd("slice")}.

  • slice_head(): \Sexpr[stage=render,results=rd]{dplyr:::methods_rd("slice_head")}.

  • slice_tail(): \Sexpr[stage=render,results=rd]{dplyr:::methods_rd("slice_tail")}.

  • slice_min(): \Sexpr[stage=render,results=rd]{dplyr:::methods_rd("slice_min")}.

  • slice_max(): \Sexpr[stage=render,results=rd]{dplyr:::methods_rd("slice_max")}.

  • slice_sample(): \Sexpr[stage=render,results=rd]{dplyr:::methods_rd("slice_sample")}.

See Also

Other single table verbs: mutate(), rename(), summarise()

Examples

data(pasilla)
pasilla |> slice(1)


stemangiola/tidySE documentation built on May 18, 2024, 1:22 p.m.