Choose rows by their position

# S3 method for class 'Ranges'
slice(.data, ..., .preserve = FALSE)

# S3 method for class 'GroupedGenomicRanges'
slice(.data, ..., .preserve = FALSE)

# S3 method for class 'GroupedIntegerRanges'
slice(.data, ..., .preserve = FALSE)

Arguments

.data

a Ranges object

...

Integer row values indicating rows to keep. If .data has been grouped via group_by.GenomicRanges(), then the positions are selected within each group.

.preserve

when FALSE (the default) the grouping structure is recomputed, otherwise it is kept as is. Currently ignored.

Value

a GRanges object

Examples

df <- data.frame(start = 1:10,
                 width = 5,
                 seqnames = "seq1",
                 strand = sample(c("+", "-", "*"), 10, replace = TRUE),
                 gc = runif(10))
rng <- as_granges(df)
dplyr::slice(rng, 1:2)
#> GRanges object with 2 ranges and 1 metadata column:
#>       seqnames    ranges strand |        gc
#>          <Rle> <IRanges>  <Rle> | <numeric>
#>   [1]     seq1       1-5      - |  0.315184
#>   [2]     seq1       2-6      + |  0.282670
#>   -------
#>   seqinfo: 1 sequence from an unspecified genome; no seqlengths
dplyr::slice(rng, -n())
#> GRanges object with 9 ranges and 1 metadata column:
#>       seqnames    ranges strand |        gc
#>          <Rle> <IRanges>  <Rle> | <numeric>
#>   [1]     seq1       1-5      - | 0.3151841
#>   [2]     seq1       2-6      + | 0.2826696
#>   [3]     seq1       3-7      * | 0.7131957
#>   [4]     seq1       4-8      * | 0.8621379
#>   [5]     seq1       5-9      * | 0.1063235
#>   [6]     seq1      6-10      + | 0.5503278
#>   [7]     seq1      7-11      * | 0.6483695
#>   [8]     seq1      8-12      * | 0.2347867
#>   [9]     seq1      9-13      * | 0.0762972
#>   -------
#>   seqinfo: 1 sequence from an unspecified genome; no seqlengths
dplyr::slice(rng, -5:-n())
#> GRanges object with 4 ranges and 1 metadata column:
#>       seqnames    ranges strand |        gc
#>          <Rle> <IRanges>  <Rle> | <numeric>
#>   [1]     seq1       1-5      - |  0.315184
#>   [2]     seq1       2-6      + |  0.282670
#>   [3]     seq1       3-7      * |  0.713196
#>   [4]     seq1       4-8      * |  0.862138
#>   -------
#>   seqinfo: 1 sequence from an unspecified genome; no seqlengths

by_strand <- group_by(rng, strand)

# slice with group by finds positions within each group
dplyr::slice(by_strand, n())
#> GRanges object with 3 ranges and 1 metadata column:
#> Groups: strand [3]
#>       seqnames    ranges strand |        gc
#>          <Rle> <IRanges>  <Rle> | <numeric>
#>   [1]     seq1       1-5      - | 0.3151841
#>   [2]     seq1      9-13      * | 0.0762972
#>   [3]     seq1     10-14      + | 0.9230950
#>   -------
#>   seqinfo: 1 sequence from an unspecified genome; no seqlengths
dplyr::slice(by_strand, which.max(gc))
#> GRanges object with 3 ranges and 1 metadata column:
#> Groups: strand [3]
#>       seqnames    ranges strand |        gc
#>          <Rle> <IRanges>  <Rle> | <numeric>
#>   [1]     seq1       1-5      - |  0.315184
#>   [2]     seq1       4-8      * |  0.862138
#>   [3]     seq1     10-14      + |  0.923095
#>   -------
#>   seqinfo: 1 sequence from an unspecified genome; no seqlengths

# if the index is beyond the number of groups slice are ignored
dplyr::slice(by_strand, 1:3)
#> GRanges object with 7 ranges and 1 metadata column:
#> Groups: strand [3]
#>       seqnames    ranges strand |        gc
#>          <Rle> <IRanges>  <Rle> | <numeric>
#>   [1]     seq1       1-5      - |  0.315184
#>   [2]     seq1       2-6      + |  0.282670
#>   [3]     seq1       3-7      * |  0.713196
#>   [4]     seq1       4-8      * |  0.862138
#>   [5]     seq1       5-9      * |  0.106324
#>   [6]     seq1      6-10      + |  0.550328
#>   [7]     seq1     10-14      + |  0.923095
#>   -------
#>   seqinfo: 1 sequence from an unspecified genome; no seqlengths