slice {tidyseurat} | R 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()
andslice_tail()
select the first or last rows. -
slice_sample()
randomly selects rows. -
slice_min()
andslice_max()
select rows with highest or lowest 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 'Seurat'
slice(.data, ..., .by = NULL, .preserve = FALSE)
## S3 method for class 'Seurat'
slice_sample(
.data,
...,
n = NULL,
prop = NULL,
by = NULL,
weight_by = NULL,
replace = FALSE
)
## S3 method for class 'Seurat'
slice_head(.data, ..., n, prop, by = NULL)
## S3 method for class 'Seurat'
slice_tail(.data, ..., n, prop, by = NULL)
## S3 method for class 'Seurat'
slice_min(
.data,
order_by,
...,
n,
prop,
by = NULL,
with_ties = TRUE,
na_rm = FALSE
)
## S3 method for class 'Seurat'
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 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 |
.by , by |
< |
.preserve |
Relevant when the |
n , prop |
Provide either A negative value of |
weight_by |
< |
replace |
Should sampling be performed with ( |
order_by |
< |
with_ties |
Should ties be kept together? The default, |
na_rm |
Should missing values in |
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()
: no methods found. -
slice_head()
: no methods found. -
slice_tail()
: no methods found. -
slice_min()
: no methods found. -
slice_max()
: no methods found. -
slice_sample()
: no methods found.
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()
: no methods found. -
slice_head()
: no methods found. -
slice_tail()
: no methods found. -
slice_min()
: no methods found. -
slice_max()
: no methods found. -
slice_sample()
: no methods found.
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()
: no methods found. -
slice_head()
: no methods found. -
slice_tail()
: no methods found. -
slice_min()
: no methods found. -
slice_max()
: no methods found. -
slice_sample()
: no methods found.
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()
: no methods found. -
slice_head()
: no methods found. -
slice_tail()
: no methods found. -
slice_min()
: no methods found. -
slice_max()
: no methods found. -
slice_sample()
: no methods found.
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()
: no methods found. -
slice_head()
: no methods found. -
slice_tail()
: no methods found. -
slice_min()
: no methods found. -
slice_max()
: no methods found. -
slice_sample()
: no methods found.
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()
: no methods found. -
slice_head()
: no methods found. -
slice_tail()
: no methods found. -
slice_min()
: no methods found. -
slice_max()
: no methods found. -
slice_sample()
: no methods found.
See Also
Other single table verbs:
arrange()
,
mutate()
,
rename()
,
summarise()
Examples
data(pbmc_small)
pbmc_small |> slice(1)
# Slice group-wise using .by
pbmc_small |> slice(1:2, .by=groups)
# slice_sample() allows you to random select with or without replacement
pbmc_small |> slice_sample(n=5)
# if using replacement, and duplicate cells are returned, a tibble will be
# returned because duplicate cells cannot exist in Seurat objects
pbmc_small |> slice_sample(n=1, replace=TRUE) # returns Seurat
pbmc_small |> slice_sample(n=100, replace=TRUE) # returns tibble
# weight by a variable
pbmc_small |> slice_sample(n=5, weight_by=nCount_RNA)
# sample by group
pbmc_small |> slice_sample(n=5, by=groups)
# sample using proportions
pbmc_small |> slice_sample(prop=0.10)
# First rows based on existing order
pbmc_small |> slice_head(n=5)
# Last rows based on existing order
pbmc_small |> slice_tail(n=5)
# 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)
# Rows with minimum and maximum values of a metadata variable
pbmc_small |> slice_max(nFeature_RNA, n=5)