Skip to contents

Subset rows in ir objects using their positions

Usage

slice.ir(.data, ..., .preserve = FALSE)

slice_sample.ir(.data, ..., n, prop, weight_by = NULL, replace = FALSE)

Arguments

.data

An object of class ir.

...

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_helpers(), 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.

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 a negative value of n or prop is provided, the specified number or proportion of rows will be removed.

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 does not yield an integer number of rows, the absolute value of prop*nrow(.data) is rounded down.

weight_by

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

.data with subsetted rows.

Examples

## slice
dplyr::slice(ir_sample_data, 1:5)
#> # A tibble: 5 × 7
#>   id_measurement id_sample sample_type sample_comment              klason_lignin
#> *          <int> <chr>     <chr>       <chr>                                 [1]
#> 1              1 GN 11-389 needles     Abies Firma Momi fir                0.360
#> 2              2 GN 11-400 needles     Cupressocyparis leylandii …         0.339
#> 3              3 GN 11-407 needles     Juniperus chinensis Chines…         0.268
#> 4              4 GN 11-411 needles     Metasequoia glyptostroboid…         0.350
#> 5              5 GN 11-416 needles     Pinus strobus Torulosa              0.331
#> # … with 2 more variables: holocellulose [1], spectra <named list>
dplyr::slice_min(ir_sample_data, holocellulose, n = 3)
#> # A tibble: 3 × 7
#>   id_measurement id_sample sample_type        sample_comment       klason_lignin
#> *          <int> <chr>     <chr>              <chr>                          [1]
#> 1              8 GN 11-423 needles            Taxodium distichum …         0.357
#> 2             32 LG 11-418 leaves and grasses Prunus incisa Fuji …         0.326
#> 3             33 LG 11-432 leaves and grasses Acer rubrum red map…         0.239
#> # … with 2 more variables: holocellulose [1], spectra <named list>
dplyr::slice_max(ir_sample_data, holocellulose, n = 3)
#> # A tibble: 3 × 7
#>   id_measurement id_sample  sample_type             sample_comment klason_lignin
#> *          <int> <chr>      <chr>                   <chr>                    [1]
#> 1             42 OCC 11-457 old corrugated cardboa… "OCC 1"                0.102
#> 2             43 OCC 11-462 old corrugated cardboa… "OCC 3"                0.142
#> 3             56 OFF 13-144 office paper            ""                     0.318
#> # … with 2 more variables: holocellulose [1], spectra <named list>
dplyr::slice_head(ir_sample_data, n = 5)
#> # A tibble: 5 × 7
#>   id_measurement id_sample sample_type sample_comment              klason_lignin
#> *          <int> <chr>     <chr>       <chr>                                 [1]
#> 1              1 GN 11-389 needles     Abies Firma Momi fir                0.360
#> 2              2 GN 11-400 needles     Cupressocyparis leylandii …         0.339
#> 3              3 GN 11-407 needles     Juniperus chinensis Chines…         0.268
#> 4              4 GN 11-411 needles     Metasequoia glyptostroboid…         0.350
#> 5              5 GN 11-416 needles     Pinus strobus Torulosa              0.331
#> # … with 2 more variables: holocellulose [1], spectra <named list>
dplyr::slice_tail(ir_sample_data, n = 5)
#> # A tibble: 5 × 7
#>   id_measurement id_sample  sample_type   sample_comment      klason_lignin
#> *          <int> <chr>      <chr>         <chr>                         [1]
#> 1             54 ONP 11-459 old newsprint "Los Angeles Times"        0.195 
#> 2             55 OFF 10-506 office paper  ""                         0.204 
#> 3             56 OFF 13-144 office paper  ""                         0.318 
#> 4             57 OFF 08-80  office paper  ""                         0.562 
#> 5             58 OFF 08-852 office paper  ""                         0.0542
#> # … with 2 more variables: holocellulose [1], spectra <named list>

## slice_sample
set.seed(234)
dplyr::slice_sample(ir_sample_data, n = 3)
#> # A tibble: 3 × 7
#>   id_measurement id_sample sample_type        sample_comment       klason_lignin
#> *          <int> <chr>     <chr>              <chr>                          [1]
#> 1             33 LG 11-432 leaves and grasses Acer rubrum red map…         0.239
#> 2             31 LG 11-414 leaves and grasses Nyssa sylvatica Bla…         0.219
#> 3             34 LG 11-433 leaves and grasses Cornus florida Flow…         0.181
#> # … with 2 more variables: holocellulose [1], spectra <named list>