[Superseded] sample_n() and sample_frac() have been superseded in favour of slice_sample(). While they will not be deprecated in the near future, retirement means that we will only perform critical bug fixes, so we recommend moving to the newer alternative.

These functions were superseded because we realised it was more convenient to have two mutually exclusive arguments to one function, rather than two separate functions. This also made it to clean up a few other smaller design issues with sample_n()/sample_frac:

  • The connection to slice() was not obvious.

  • The name of the first argument, tbl, is inconsistent with other single table verbs which use .data.

  • The size argument uses tidy evaluation, which is surprising and undocumented.

  • It was easier to remove the deprecated .env argument.

  • ... was in a suboptimal position.

# S3 method for class 'SingleCellExperiment'
sample_n(tbl, size, replace = FALSE, weight = NULL, .env = NULL, ...)

# S3 method for class 'SingleCellExperiment'
sample_frac(tbl, size = 1, replace = FALSE, weight = NULL, .env = NULL, ...)

Arguments

tbl

A data.frame.

size

<tidy-select> For sample_n(), the number of rows to select. For sample_frac(), the fraction of rows to select. If tbl is grouped, size applies to each group.

replace

Sample with or without replacement?

weight

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

.env

DEPRECATED.

...

ignored

Value

`tidySingleCellExperiment`

References

Hutchison, W.J., Keyes, T.J., The tidyomics Consortium. et al. The tidyomics ecosystem: enhancing omic data analyses. Nat Methods 21, 1166–1170 (2024). https://doi.org/10.1038/s41592-024-02299-2

Wickham H, Averick M, Bryan J, Chang W, McGowan LD, François R, et al. Welcome to the tidyverse. Journal of Open Source Software. 2019;4(43):1686. https://doi.org/10.21105/joss.01686

Examples

data(pbmc_small)
pbmc_small |> sample_n(50)
#> # A SingleCellExperiment-tibble abstraction: 50 × 17
#> # Features=230 | Cells=50 | Assays=counts, logcounts
#>    .cell orig.ident nCount_RNA nFeature_RNA RNA_snn_res.0.8 letter.idents groups
#>    <chr> <fct>           <dbl>        <int> <fct>           <fct>         <chr> 
#>  1 CGTA… SeuratPro…        371           75 1               B             g1    
#>  2 TTGC… SeuratPro…        104           40 0               A             g2    
#>  3 GACA… SeuratPro…        872           96 1               B             g1    
#>  4 ATTA… SeuratPro…        463           77 1               B             g1    
#>  5 GTTG… SeuratPro…        221           67 0               A             g2    
#>  6 GCAG… SeuratPro…         72           45 0               A             g1    
#>  7 CTAA… SeuratPro…        189           53 0               A             g1    
#>  8 CCAT… SeuratPro…        224           50 1               B             g2    
#>  9 CGGC… SeuratPro…         94           55 0               A             g2    
#> 10 TACT… SeuratPro…        156           48 0               A             g1    
#> # ℹ 40 more rows
#> # ℹ 10 more variables: RNA_snn_res.1 <fct>, file <chr>, ident <fct>,
#> #   PC_1 <dbl>, PC_2 <dbl>, PC_3 <dbl>, PC_4 <dbl>, PC_5 <dbl>, tSNE_1 <dbl>,
#> #   tSNE_2 <dbl>
pbmc_small |> sample_frac(0.1)
#> # A SingleCellExperiment-tibble abstraction: 8 × 17
#> # Features=230 | Cells=8 | Assays=counts, logcounts
#>   .cell  orig.ident nCount_RNA nFeature_RNA RNA_snn_res.0.8 letter.idents groups
#>   <chr>  <fct>           <dbl>        <int> <fct>           <fct>         <chr> 
#> 1 TGGTA… SeuratPro…         64           36 0               A             g1    
#> 2 GCGCA… SeuratPro…        213           48 1               B             g2    
#> 3 AGATA… SeuratPro…        187           61 0               A             g2    
#> 4 GGCAT… SeuratPro…        172           29 0               A             g1    
#> 5 ATCAT… SeuratPro…        168           37 0               A             g2    
#> 6 CGTAG… SeuratPro…        371           75 1               B             g1    
#> 7 GCACT… SeuratPro…        292           71 1               B             g2    
#> 8 ACAGG… SeuratPro…        151           59 0               A             g1    
#> # ℹ 10 more variables: RNA_snn_res.1 <fct>, file <chr>, ident <fct>,
#> #   PC_1 <dbl>, PC_2 <dbl>, PC_3 <dbl>, PC_4 <dbl>, PC_5 <dbl>, tSNE_1 <dbl>,
#> #   tSNE_2 <dbl>