You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

igraph传递性计算的顶点索引匹配与指定顶点筛选问题

问题解决方法

问题1:transitivity结果与顶点匹配

  • transitivity()返回的local聚类系数向量顺序,和图对象的顶点列表V(g)的顺序完全对应,每个结果对应的顶点ID就是V(g)$name的值,直接将两者绑定即可得到对应关系。
    单个图的匹配示例:
# 取列表中第一个图做示例
g <- gemails[[1]]
tran_vals <- transitivity(g, type = "local")
# 生成顶点ID与聚类系数的对应表
tran_df <- data.frame(
  vertex_id = as.integer(V(g)$name),
  transitivity = tran_vals
)

问题2:仅计算ID小于指定值的顶点transitivity

你之前传入1:4000不生效是因为该数值是igraph内部的顶点顺序索引,不是你原始数据里的顶点ID数值。需要先筛选出name属性(即原始from/to列的数值)小于阈值的顶点,再传入vids参数即可。
筛选计算示例(阈值设为4000):

threshold <- 4000
# 筛选符合条件的顶点
target_vertices <- V(g)[as.integer(V(g)$name) < threshold]
# 仅对目标顶点计算transitivity
tran_target <- transitivity(g, type = "local", vids = target_vertices)
# 生成对应结果表
tran_target_df <- data.frame(
  vertex_id = as.integer(target_vertices$name),
  transitivity = tran_target
)

完整处理全列表的修正代码

library(igraph)
library(dplyr)

# 原始预处理步骤,增加.groups参数避免dplyr提示
emails <- lapply(ldf, function(x) {
  x %>% 
    group_by(from, to) %>% 
    summarize(weight = n(), .groups = "drop")
})
gemails <- lapply(emails, function(x) {
  graph_from_data_frame(x, directed = TRUE)
})

# 批量计算所有图的目标顶点transitivity,自动匹配ID
threshold <- 4000
clustering_with_id <- lapply(gemails, function(g) {
  target_v <- V(g)[as.integer(V(g)$name) < threshold]
  tran_vals <- transitivity(g, type = "local", vids = target_v)
  return(data.frame(
    vertex_id = as.integer(target_v$name),
    transitivity = tran_vals
  ))
})

最终输出的clustering_with_id是和原始gemails列表一一对应的结果列表,每个元素为当前图内所有ID小于阈值的顶点和对应transitivity的匹配表。

内容的提问来源于stack exchange,提问作者Usuario5678

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 05:54:06