英文:
iterating a for loop over a list of igraph objects to compare different clustering algorithms
问题
以下是您要翻译的部分:
"我在想是否有人可以帮我解决这个for循环的问题。或者建议另一种获取我想要的方式(我知道代码有点混乱,但我对如何使它更加优雅感到困惑)。
问题:我有一个包含一些网络对象的列表;我想比较一些用于这些网络的社区检测算法。而且我希望一次性获取到这些信息(不像这里的玩具示例那样,我实际的列表中有许多网络)。以下的玩具示例让我接近我想要的信息,但它没有在最终输出中列出网络的名称。为了在最终的数据框中也包括网络的名称,我需要做些什么?
library(graph)
library(igraphdata)
library(igraph)
# 创建一些网络
data(karate)
g2 <- erdos.renyi.game(50, .2)
# 把网络放入一个列表中
list_of_networks <- list(karate, g2)
# 获取所有的社区检测算法
clusters <- grep("^cluster_", ls("package:igraph"), value=TRUE)[-1]
# 去掉我不想要的算法
clusters <- clusters[!grepl("spinglass|fluid|leiden|walktrap|infomap|prop|optimal", clusters)]
# for循环部分
# 列表中有两个网络
x <- 1:2
# 一个用于for循环结果的容器
container <- vector("list", length = 2)
# 嵌套的for循环
for (cluster in clusters) {
for (i in x) {
result <- t(c(
modularity(do.call(cluster, list(list_of_networks[[i]])),
length(do.call(cluster, list(list_of_networks[[i]])),
cluster
))
container[[i]] <- rbind(result, container[[i]])
}
}
container %>%
map(~ as_tibble(.)) %>%
bind_rows() %>%
rename(modularity = 1,
num_communities = 2,
algorithm = 3)
"
英文:
I’m wondering if someone could help me with this for loop. Or suggest another way of getting at what I want (I know the code is a bit of a nightmare, but I’m stumped on how to make it, well, more elegant).
The problem: I have some network objects in a list; I would like to compare some community detection algorithms for those networks. And I would like to get at that information in one go (unlike the toy example here the real list I have has many networks in it). The following toy example gets me close to the information I want, but it does not list the networks in the final output. What do I need to do in order to also include the name of the networks in the final data frame?
library(graph)
library(igraphdata)
library(igraph)
# create a couple of networks
data(karate)
g2 <- erdos.renyi.game(50, .2)
# stick the networks in a list
list_of_networks <- list(karate, g2)
# get all the community detection algorithms
clusters <- grep("^cluster_", ls("package:igraph"), value=TRUE)[-1]
# get rid of the algorithms I don't want
clusters <- clusters[!grepl("spinglass|fluid|leiden|walktrap|infomap|prop|optimal", clusters)]
# for loop stuff
# there are two networks in the network list
x <- 1:2
# a container for my for loop results
container <- vector("list", length = 2)
# nested for loop
for (cluster in clusters) {
for (i in x) {
result <- t(c(
modularity(do.call(cluster, list(list_of_networks[[i]]))),
length(do.call(cluster, list(list_of_networks[[i]]))),
cluster
))
container[[i]] <- rbind(result, container[[i]])
}
}
container %>%
map(~ as_tibble(.)) %>%
bind_rows() %>%
rename(modularity = 1,
num_communities = 2,
algorithm = 3)
答案1
得分: 0
You could use map_df
's .id
argument:
library(tidyverse)
container %>%
map_df(~ as_tibble(.) %>%
rename(modularity = 1,
num_communities = 2,
algorithm = 3),
.id = "name")
This returns
# A tibble: 6 × 4
name modularity num_communities algorithm
<chr> <chr> <chr> <chr>
1 1 0.443854125672307 4 cluster_louvain
2 1 0.4366391184573 5 cluster_leading_eigen
3 1 0.434521466989 3 cluster_fast_greedy
4 2 0.224687153369057 5 cluster_louvain
5 2 0.197245699823462 4 cluster_leading_eigen
6 2 0.193293130653502 4 cluster_fast_greedy
so your networks are named 1
and 2
.
To get better information, we add a line to your code:
container <- vector("list", length = 2)
# Add names here
names(container) <- unlist(lapply(list_of_networks, \(x) x$name))
# or
# names(container) <- sapply(list_of_networks, \(x) x$name)
Now
container %>%
map_df(~ as_tibble(.) %>%
rename(modularity = 1,
num_communities = 2,
algorithm = 3),
.id = "name")
returns
# A tibble: 6 × 4
name modularity num_communities algorithm
<chr> <chr> <chr> <chr>
1 Zachary's karate club network 0.444903581267218 4 cluster_louvain
2 Zachary's karate club network 0.4366391184573 5 cluster_leading_eigen
3 Zachary's karate club network 0.434521466989 3 cluster_fast_greedy
4 Erdos-Renyi (gnp) graph 0.210112108800672 4 cluster_louvain
5 Erdos-Renyi (gnp) graph 0.188379542113001 2 cluster_leading_eigen
6 Erdos-Renyi (gnp) graph 0.21151675068263 3 cluster_fast_greedy
英文:
You could use map_df
's .id
argument:
library(tidyverse)
container %>%
map_df(~ as_tibble(.) %>%
rename(modularity = 1,
num_communities = 2,
algorithm = 3),
.id = "name")
This returns
# A tibble: 6 × 4
name modularity num_communities algorithm
<chr> <chr> <chr> <chr>
1 1 0.443854125672307 4 cluster_louvain
2 1 0.4366391184573 5 cluster_leading_eigen
3 1 0.434521466989 3 cluster_fast_greedy
4 2 0.224687153369057 5 cluster_louvain
5 2 0.197245699823462 4 cluster_leading_eigen
6 2 0.193293130653502 4 cluster_fast_greedy
so your networks are named 1
and 2
.
To get better information, we add a line to your code
container <- vector("list", length = 2)
# Add names here
names(container) <- unlist(lapply(list_of_networks, \(x) x$name))
# or
# names(container) <- sapply(list_of_networks, \(x) x$name)
Now
container %>%
map_df(~ as_tibble(.) %>%
rename(modularity = 1,
num_communities = 2,
algorithm = 3),
.id = "name")
returns
# A tibble: 6 × 4
name modularity num_communities algorithm
<chr> <chr> <chr> <chr>
1 Zachary's karate club network 0.444903581267218 4 cluster_louvain
2 Zachary's karate club network 0.4366391184573 5 cluster_leading_eigen
3 Zachary's karate club network 0.434521466989 3 cluster_fast_greedy
4 Erdos-Renyi (gnp) graph 0.210112108800672 4 cluster_louvain
5 Erdos-Renyi (gnp) graph 0.188379542113001 2 cluster_leading_eigen
6 Erdos-Renyi (gnp) graph 0.21151675068263 3 cluster_fast_greedy
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论