I'm trying to bin a symmetric matrix with irregular intervals in R but am not sure how to proceed. My ideas are:
- Reshape the matrix to long format, aggregate and cast it back?
- Bin as-is in both dimensions (somehow... tapply, aggregate?)
- Keep the regular binning but for each of my (larger) irregular bins, replace all inner values with their sum?
Here's an example of what I'm trying to do:
set.seed(42)
# symmetric matrix
a <- matrix(rpois(1e4, 2), 100)
a[upper.tri(a)] <- t(a)[upper.tri(a)]
image(x=1:100, y=1:100, a, asp=1, frame=F, axes=F)
# vector of irregular breaks for binning
breaks <- c(12, 14, 25, 60, 71, 89)
# white line show the desired bins
abline(h=breaks-.5, lwd=2, col="white")
abline(v=breaks-.5, lwd=2, col="white")
(The aim being that each rectangle drawn above be filled according to the sum of values within it.) I'd appreciate any pointers of how best to approach this.
This answer provides a great starting point using
tapply
:These can then be plotted as rectangular bins using a base plot and
rect
— i.e.: