make 循环在 R 中创建 igraph 对象列表
我想创建一个 Igraph 对象列表,其中每个 Igraph 对象使用的数据由另一个变量确定。
这就是我创建单个 Igraph 对象的方法
netEdges <- NULL
for (idi in c("nom1", "nom2", "nom3")) {
netEdge <- net[c("id", idi)]
names(netEdge) <- c("id", "friendID")
netEdge$weight <- 1
netEdges <- rbind(netEdges, netEdge)
}
g <- graph.data.frame(netEdges, directed=TRUE)
对于 net$community
的每个唯一值,我想创建一个新的 Igraph 对象。然后我想计算每个对象的中心性度量,然后将这些度量带回到我的 net
数据集中。非常感谢您的帮助!
I'd like to create a list of Igraph objects with the data used for each Igraph object determined by another variable.
This is how I create a single Igraph object
netEdges <- NULL
for (idi in c("nom1", "nom2", "nom3")) {
netEdge <- net[c("id", idi)]
names(netEdge) <- c("id", "friendID")
netEdge$weight <- 1
netEdges <- rbind(netEdges, netEdge)
}
g <- graph.data.frame(netEdges, directed=TRUE)
For each unique value of net$community
I'd like to make a new Igraph object. Then I would like to calculate measures of centrality for each object and then bring those measures back into my net
dataset. Many thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您提供的代码不能完全重现,因此不能保证下面的代码能够运行。它旨在作为如何构建实际解决方案的指南。如果您提供其他人可以用来运行您的代码的示例数据,您将得到更好的答案。
最简单的方法可能是将
net
拆分为一个列表,其中每个元素对应于community
的每个唯一值,然后将图形构建代码应用于每个部分,存储另一个列表中每件作品的结果。在 R 中,有多种方法可以完成此类操作,其中之一是使用lapply
:如果一切顺利,
result
应该是一个包含图形的列表 (或您修改myFun
以返回的任何内容)对于community
的每个唯一值。用于执行类似任务的其他流行工具包括plyr
包中的ddply
。Since the code you provide isn't completely reproducible, what follows is not guaranteed to run. It is intended as a guide for how to structure a real solution. If you provide example data that others can use to run your code, you will get better answers.
The simplest way to do this is probably to split
net
into a list with one element for each unique value ofcommunity
and then apply your graph building code to each piece, storing the results for each piece in another list. There are several ways to doing this type of thing in R, one of which is to uselapply
:If all has gone well,
result
should be a list containing a graph (or whatever you modifiedmyFun
to return) for each unique value ofcommunity
. Other popular tools for doing similar tasks includeddply
from theplyr
package.