This is a generalization of GenomicRanges::binnedAverage to allow the use of any applicable function instead of simply the mean. The interface is very similar, with two differences:

  1. Added argument fun, for the function to use.

  2. Dropped argument varname, since this function outputs the metadata as a vector, rather than the complete GRanges object with the metadata column added and named as indicated by varname.

binned_function(bins, numvar, fun, ...)



GRanges object representing the genomic bins. Typically obtained by calling tileGenome with No default.


Named RleList object representing a numerical variable defined along the genome covered by bins (which is the genome described by seqinfo(bins)). No default.


R function to apply to the numerical variable in each bin. No default.


optional arguments to fun.


# Generate 100-bp genome tiles for a given GRanges object
bins <- GenomicRanges::tileGenome(GenomeInfoDb::seqlengths(gr),

# Get signal as "RleList"; stored in the "score" metadata column
score <- GenomicRanges::coverage(gr, weight="score")

# Now use the function to get the maximum score in each tile
gr$max_per_bin <- binned_function(bins=bins, numvar=score, fun=max)

# You can pass arguments to fun too
gr$max_per_bin <- binned_function(bins=bins, numvar=score,
                                  fun=max, na.rm=TRUE)
# }