I have an adjacency matrix that expresses feeding links between species (column eats row)

```
mat1<-matrix(data=c(0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0), 
                nrow=4, 
                ncol=4, 
                byrow = TRUE,
                dimnames = list(c("a","b","c","d"), 
                                c("a","b","c","d")))

```

I want to reduce the resolution of this matrix to family level using a dataframe that shows which family each species belongs to,

```
df <- data.frame(Species = c("a","b","c","d"), Family = c("E","E","F","F"))

```

so that the resulting matrix will give the number of feeding links between families

```
mat2<-matrix(data=c(0,2,1,0), 
            nrow=2, 
            ncol=2, 
            byrow = TRUE,
            dimnames = list(c("E","F"), 
                            c("E","F")))

```

Thank you for your time