如何通过节点分组来降低相邻矩阵的分辨率。

问题描述 投票:0回答:1

我有一个邻接矩阵,表达物种之间的喂养联系(列吃行)。

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")))

我想用一个数据框将这个矩阵的分辨率降低到科级,以显示每个物种属于哪个科。

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

因此,所得到的矩阵将给出家庭之间的喂养环节数量

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

谢谢你的时间

r adjacency-matrix
1个回答
1
投票

因为这是我唯一知道的方法,这里有一个使用tidyverse的解决方案。它把矩阵变成一个长形的tibble,按族聚合,然后再把它变宽。

library(tidyverse)

# create a tibble that looks like the desired end-result matrix
df2 <- mat1 %>% 
  as_tibble(rownames = "Species_from") %>% # make a tibble
  pivot_longer(cols = -Species_from,
               names_to = "Species_to") %>% # turn into long form
  left_join(df, by = c("Species_from" = "Species")) %>% # add Family_from and Family_to
  left_join(df, by = c("Species_to" = "Species"), suffix = c("_from", "_to")) %>% 
  group_by(Family_from, Family_to) %>% # aggregate Family_from and Family_to
  summarise(value = sum(value)) %>% # ... by taking their sum
  pivot_wider(names_from = Family_to,
              values_from = value) # turn back into wide form

# turn into a matrix
mat2 <- as.matrix(df2[, c("E", "F")])
rownames(mat2) <- df2$Family_from

mat2

#   E F
# E 0 2
# F 1 0

2
投票

我相信还有更优雅的方法,但这里有一个方法,用的是 data.table. 如果您的邻接矩阵非常大,这可能比来自于 tidyr.

首先,我们将这两个对象转换为 data.tables. 然后我们加入 Family 到相邻矩阵上。然后,我们将每一列按 Family 组。最后,我们换位思考,再做同样的事情。

library(data.table)
setDT(df)
dt <- as.data.table(cbind(Species = rownames(mat1),as.data.frame(mat1)))
a <- df[dt,on = "Species"][,-"Species"][,lapply(.SD, sum), by = Family]
b <- data.table::transpose(a, keep.names = "Family", make.names = 1)
setnames(b,"Family","Species")
c <- df[b,on = "Species"][,-"Species"][,lapply(.SD,sum), by = Family]
data.table::transpose(c, keep.names = "Family", make.names = 1)
   Family E F
1:      E 0 2
2:      F 1 0
© www.soinside.com 2019 - 2024. All rights reserved.