Hi, thanks a lot. tapply() is incredibly useful to add to my arsenal. I updated post with example data, little improvements and my output. How would I make sure to get the NAs for non overlapping reference regions and keeping the chromosome locations?
This is the only way I could think of:
ovlp <- findOverlaps(ref.gr[y],peaks.gr,ignore.strand=T)
y <- apply(peaks[subjectHits(ovlp),4:5],2,function(x) tapply(x, factor(queryHits(ovlp)), mean))
y <- cbind(ref[unique(queryHits(ovlp)),],t(data.frame(y)))
y2 <- ref[-unique(queryHits(ovlp)),]
y2 <- cbind(y2,NA,NA)
colnames(y2) <- colnames(y)
rbind(y,y2)
Not reproducible, please provide some example data and expected output.
Added, sorry.
It probably would be fatster to use bedtools.