Code from slides

library(GenomicRanges)
Loading required package: stats4
Loading required package: BiocGenerics

Attaching package: 'BiocGenerics'
The following objects are masked from 'package:stats':

    IQR, mad, sd, var, xtabs
The following objects are masked from 'package:base':

    anyDuplicated, aperm, append, as.data.frame, basename, cbind,
    colnames, dirname, do.call, duplicated, eval, evalq, Filter, Find,
    get, grep, grepl, intersect, is.unsorted, lapply, Map, mapply,
    match, mget, order, paste, pmax, pmax.int, pmin, pmin.int,
    Position, rank, rbind, Reduce, rownames, sapply, setdiff, sort,
    table, tapply, union, unique, unsplit, which.max, which.min
Loading required package: S4Vectors

Attaching package: 'S4Vectors'
The following object is masked from 'package:utils':

    findMatches
The following objects are masked from 'package:base':

    expand.grid, I, unname
Loading required package: IRanges
Loading required package: GenomeInfoDb
library(plyranges)

Attaching package: 'plyranges'
The following object is masked from 'package:IRanges':

    slice
The following object is masked from 'package:stats':

    filter
gr <- GRanges(seqnames = "chr1", 
              strand = c("+", "-"), 
              ranges = IRanges(start = c(102012, 520211), 
                               end = c(120303, 526211)),
              gene_id = c(1001, 2151), 
              score = c(10, 25))
gr
GRanges object with 2 ranges and 2 metadata columns:
      seqnames        ranges strand |   gene_id     score
         <Rle>     <IRanges>  <Rle> | <numeric> <numeric>
  [1]     chr1 102012-120303      + |      1001        10
  [2]     chr1 520211-526211      - |      2151        25
  -------
  seqinfo: 1 sequence from an unspecified genome; no seqlengths
width(gr)
[1] 18292  6001
gr[gr$score > 15, ]
GRanges object with 1 range and 2 metadata columns:
      seqnames        ranges strand |   gene_id     score
         <Rle>     <IRanges>  <Rle> | <numeric> <numeric>
  [1]     chr1 520211-526211      - |      2151        25
  -------
  seqinfo: 1 sequence from an unspecified genome; no seqlengths
gr |> 
  filter(score > 15)
GRanges object with 1 range and 2 metadata columns:
      seqnames        ranges strand |   gene_id     score
         <Rle>     <IRanges>  <Rle> | <numeric> <numeric>
  [1]     chr1 520211-526211      - |      2151        25
  -------
  seqinfo: 1 sequence from an unspecified genome; no seqlengths
gr |> 
  filter(score > 15) |> 
  width()
[1] 6001
library(plyranges)
gr <- 
  data.frame(seqnames = sample(c("chr1", "chr2"), 7, replace = TRUE),
             strand = sample(c("+", "-"), 7, replace = TRUE),
             score = runif(7),
             start = 1:7,
             width = 10) %>%
  as_granges()
gr
GRanges object with 7 ranges and 1 metadata column:
      seqnames    ranges strand |     score
         <Rle> <IRanges>  <Rle> | <numeric>
  [1]     chr1      1-10      + |  0.519716
  [2]     chr2      2-11      - |  0.478834
  [3]     chr1      3-12      - |  0.487161
  [4]     chr2      4-13      - |  0.677381
  [5]     chr2      5-14      - |  0.181075
  [6]     chr1      6-15      - |  0.874615
  [7]     chr2      7-16      + |  0.615209
  -------
  seqinfo: 2 sequences from an unspecified genome; no seqlengths
gr %>%
  group_by(strand) %>% 
  summarize(mean_score = mean(score))
DataFrame with 2 rows and 2 columns
  strand mean_score
   <Rle>  <numeric>
1      +   0.567462
2      -   0.539813