从 igraph 中的自我网络列表计算指定顶点 ID 的传递性
Calculating transitivity for a specified vertex ids from the list of ego-networks in igraph
我需要为一大堆深度为 1.5 的以自我为中心的网络计算局部聚类系数。所有文件都是以 ego-s 命名的边缘列表,并存储在 'edgelist' 文件夹中。这是我批量导入它的代码:
datanames <- as.character(lapply(list.files("./edgelists"), FUN = function(x) strsplit(x, split="\.")[[1]][1]))
dataset <- lapply(datanames, function(x) list(assign(x, read.table(paste("./edgelists/", x, ".csv", sep=""), header=TRUE, sep=","))))
graphs <- lapply(dataset, function(dataset) graph.data.frame(dataset, directed=F, vertices=NULL))
现在我只需要计算 egos 的传递性,这些名字作为 chr 存储在 'datanames'.
看来我不能将此变量用作 vids 参数的值,1) 既不能直接使用,2) 也不能在此函数中转换为数字、双精度和整数后使用
trans <- lapply(graphs, function(graph) transitivity(graph, type = "local", vids=datanames))
因为在第一种情况下 returns 出现以下错误:
1) Error in as.igraph.vs(graph, vids) : Invalid vertex names
转换为数字类型后我得到:
2) Error in .Call("R_igraph_transitivity_local_undirected", graph, vids, :
At iterators.c:759 : Cannot create iterator, invalid vertex id, Invalid vertex id
那我该如何完成任务?
这是我同事提供的解决方案Benjamin Lind:
all_files <- list.files("./edgelists") # reading file names
datanames <- strsplit(all_files, split = "\.") # removing file extension
datanames <- sapply(datanames, "[[", 1) # getting names of egos
# Helper function to load data
fun1 <- function(x){
pathname <- paste("./edgelists/", x, ".csv", sep="")
xdf <- read.table(pathname, header = TRUE, sep=",")
return(xdf)
}
dataset <- lapply(datanames, fun1)
# Converting data to graph objects
graphs <- lapply(dataset, graph.data.frame, directed = FALSE)
# Helper function to get vertices names of ego
egovidfun <- function(vname, vgraph){
return(which(V(vgraph)$name == vname))
}
# Transitivity function for selected egos
newtransfun <- function(vid, vgraph){
return(transitivity(vgraph, type = "local", vids = vid)[1])
}
# Getting vertices for egos
egovids <- egovidfun(datanames, graphs)
# Calculating transitivity for selected egos
trans <- newtransfun(egovids, graphs)`
我需要为一大堆深度为 1.5 的以自我为中心的网络计算局部聚类系数。所有文件都是以 ego-s 命名的边缘列表,并存储在 'edgelist' 文件夹中。这是我批量导入它的代码:
datanames <- as.character(lapply(list.files("./edgelists"), FUN = function(x) strsplit(x, split="\.")[[1]][1]))
dataset <- lapply(datanames, function(x) list(assign(x, read.table(paste("./edgelists/", x, ".csv", sep=""), header=TRUE, sep=","))))
graphs <- lapply(dataset, function(dataset) graph.data.frame(dataset, directed=F, vertices=NULL))
现在我只需要计算 egos 的传递性,这些名字作为 chr 存储在 'datanames'.
看来我不能将此变量用作 vids 参数的值,1) 既不能直接使用,2) 也不能在此函数中转换为数字、双精度和整数后使用
trans <- lapply(graphs, function(graph) transitivity(graph, type = "local", vids=datanames))
因为在第一种情况下 returns 出现以下错误:
1) Error in as.igraph.vs(graph, vids) : Invalid vertex names
转换为数字类型后我得到:
2) Error in .Call("R_igraph_transitivity_local_undirected", graph, vids, :
At iterators.c:759 : Cannot create iterator, invalid vertex id, Invalid vertex id
那我该如何完成任务?
这是我同事提供的解决方案Benjamin Lind:
all_files <- list.files("./edgelists") # reading file names
datanames <- strsplit(all_files, split = "\.") # removing file extension
datanames <- sapply(datanames, "[[", 1) # getting names of egos
# Helper function to load data
fun1 <- function(x){
pathname <- paste("./edgelists/", x, ".csv", sep="")
xdf <- read.table(pathname, header = TRUE, sep=",")
return(xdf)
}
dataset <- lapply(datanames, fun1)
# Converting data to graph objects
graphs <- lapply(dataset, graph.data.frame, directed = FALSE)
# Helper function to get vertices names of ego
egovidfun <- function(vname, vgraph){
return(which(V(vgraph)$name == vname))
}
# Transitivity function for selected egos
newtransfun <- function(vid, vgraph){
return(transitivity(vgraph, type = "local", vids = vid)[1])
}
# Getting vertices for egos
egovids <- egovidfun(datanames, graphs)
# Calculating transitivity for selected egos
trans <- newtransfun(egovids, graphs)`