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 'SingleCellExperiment'
slice(.data, ..., .by = NULL, .preserve = FALSE)

## S3 method for class 'SingleCellExperiment'
slice_sample(
  .data,
  ...,
  n = NULL,
  prop = NULL,
  by = NULL,
  weight_by = NULL,
  replace = FALSE
)

## S3 method for class 'SingleCellExperiment'
slice_head(.data, ..., n, prop, by = NULL)

## S3 method for class 'SingleCellExperiment'
slice_tail(.data, ..., n, prop, by = NULL)

## S3 method for class 'SingleCellExperiment'
slice_min(
  .data,
  order_by,
  ...,
  n,
  prop,
  by = NULL,
  with_ties = TRUE,
  na_rm = FALSE
)

## S3 method for class 'SingleCellExperiment'
slice_max(
  .data,
  order_by,
  ...,
  n,
  prop,
  by = NULL,
  with_ties = TRUE,
  na_rm = 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.

.by, by

[Experimental]

<tidy-select> Optionally, a selection of columns to group by for just this operation, functioning as an alternative to group_by(). For details and examples, see ?dplyr_by.

.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.

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. prop will be rounded towards zero to generate an integer number of rows.

A negative value of n or prop will be subtracted from the group size. For example, n = -2 with a group of 5 rows will select 5 - 2 = 3 rows; prop = -0.25 with 8 rows will select 8 * (1 - 0.25) = 6 rows.

weight_by

<data-masking> 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

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

order_by

<data-masking> Variable or function of variables to order by. To order by multiple variables, wrap them in a data frame or tibble.

with_ties

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.

na_rm

Should missing values in order_by be removed from the result? If FALSE, NA values are sorted to the end (like in arrange()), so they will only be included if there are insufficient non-missing values to reach n/prop.

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")}.

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")}.

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")}.

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")}.

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")}.

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: arrange(), mutate(), rename(), summarise()

Examples

data(pbmc_small)
pbmc_small |> slice(1)

data(pbmc_small)
pbmc_small |> slice_sample(n=1)
pbmc_small |> slice_sample(prop=0.1)

data(pbmc_small)
# First rows based on existing order
pbmc_small |> slice_head(n=5)

data(pbmc_small)
# First rows based on existing order
pbmc_small |> slice_tail(n=5)

data(pbmc_small)

# Rows with minimum and maximum values of a metadata variable
pbmc_small |> slice_min(nFeature_RNA, n=5)

# slice_min() and slice_max() may return more rows than requested
# in the presence of ties.
pbmc_small |>  slice_min(nFeature_RNA, n=2)

# Use with_ties=FALSE to return exactly n matches
pbmc_small |> slice_min(nFeature_RNA, n=2, with_ties=FALSE)

# Or use additional variables to break the tie:
pbmc_small |> slice_min(tibble::tibble(nFeature_RNA, nCount_RNA), n=2)

# Use by for group-wise operations
pbmc_small |> slice_min(nFeature_RNA, n=5, by=groups)

data(pbmc_small)
# Rows with minimum and maximum values of a metadata variable
pbmc_small |> slice_max(nFeature_RNA, n=5)


stemangiola/tidySCE documentation built on Dec. 20, 2024, 10:19 p.m.