Subset to a specific number of samples.
Usage
# S3 method for class 'rbiom'
slice(.data, ..., .by = NULL, .preserve = FALSE, clone = TRUE)
# S3 method for class 'rbiom'
slice_head(.data, n, prop, by = NULL, clone = TRUE, ...)
# S3 method for class 'rbiom'
slice_tail(.data, n, prop, by = NULL, clone = TRUE, ...)
# S3 method for class 'rbiom'
slice_min(
.data,
order_by,
n,
prop,
by = NULL,
with_ties = TRUE,
na_rm = FALSE,
clone = TRUE,
...
)
# S3 method for class 'rbiom'
slice_max(
.data,
order_by,
n,
prop,
by = NULL,
with_ties = TRUE,
na_rm = FALSE,
clone = TRUE,
...
)
# S3 method for class 'rbiom'
slice_sample(
.data,
n,
prop,
by = NULL,
weight_by = NULL,
replace = FALSE,
clone = TRUE,
...
)
Arguments
- .data
An rbiom object, such as from
as_rbiom()
.- ...
For
slice()
, integer row indexes. For otherslice_*()
functions, not used. Seedplyr::slice()
.- .by, by
-
<
tidy-select
> Optionally, a selection of columns to group by for just this operation, functioning as an alternative togroup_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.- clone
Create a copy of
biom
before modifying. IfFALSE
,biom
is modified in place as a side-effect. See speed ups for use cases. Default:TRUE
- n, prop
Provide either
n
, the number of rows, orprop
, the proportion of rows to select. If neither are supplied,n = 1
will be used. Ifn
is greater than the number of rows in the group (orprop > 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
orprop
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.- 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. UseFALSE
to ignore ties, and return the firstn
rows.- na_rm
Should missing values in
order_by
be removed from the result? IfFALSE
,NA
values are sorted to the end (like inarrange()
), so they will only be included if there are insufficient non-missing values to reachn
/prop
.- 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.
Value
An rbiom object.
See also
Other transformations:
modify_metadata
,
rarefy()
,
rarefy_cols()
,
subset()
,
with()
Examples
library(rbiom)
# The last 3 samples in the metadata table.
biom <- slice_tail(hmp50, n = 3)
#> Warning: ℹ Dropping 47 samples from biom object since they are not in the new metadata:
#> "HMP01", "HMP02", "HMP03", "HMP04", "HMP05", "HMP06", "HMP07", "HMP08",
#> "HMP09", "HMP10", "HMP11", "HMP12", "HMP13", "HMP14", "HMP15", "HMP16",
#> "HMP17", "HMP18", …, "HMP46", and "HMP47".
biom$metadata
#> # A tibble: 3 × 5
#> .sample Age BMI `Body Site` Sex
#> * <chr> <dbl> <dbl> <fct> <fct>
#> 1 HMP48 24 29 Anterior nares Male
#> 2 HMP49 21 22 Mid vagina Female
#> 3 HMP50 31 20 Mid vagina Female
# The 3 oldest subjects sampled.
biom <- slice_max(hmp50, Age, n = 3)
#> Warning: ℹ Dropping 47 samples from biom object since they are not in the new metadata:
#> "HMP01", "HMP02", "HMP03", "HMP04", "HMP05", "HMP06", "HMP07", "HMP08",
#> "HMP09", "HMP10", "HMP11", "HMP12", "HMP13", "HMP14", "HMP15", "HMP16",
#> "HMP17", "HMP18", …, "HMP49", and "HMP50".
biom$metadata
#> # A tibble: 3 × 5
#> .sample Age BMI `Body Site` Sex
#> * <chr> <dbl> <dbl> <fct> <fct>
#> 1 HMP34 40 27 Anterior nares Female
#> 2 HMP37 38 31 Mid vagina Female
#> 3 HMP44 38 19 Mid vagina Female
# Pick 3 samples at random.
biom <- slice_sample(hmp50, n = 3)
#> Warning: ℹ Dropping 47 samples from biom object since they are not in the new metadata:
#> "HMP01", "HMP02", "HMP03", "HMP04", "HMP05", "HMP06", "HMP08", "HMP09",
#> "HMP10", "HMP11", "HMP12", "HMP13", "HMP14", "HMP15", "HMP16", "HMP17",
#> "HMP18", "HMP19", …, "HMP49", and "HMP50".
biom$metadata
#> # A tibble: 3 × 5
#> .sample Age BMI `Body Site` Sex
#> * <chr> <dbl> <dbl> <fct> <fct>
#> 1 HMP46 27 19 Mid vagina Female
#> 2 HMP32 28 27 Anterior nares Female
#> 3 HMP07 26 22 Buccal mucosa Male