make 循环在 R 中创建 igraph 对象列表

发布于 2024-11-26 05:31:46 字数 523 浏览 0 评论 0原文

我想创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生生不灭 2024-12-03 05:31:46

由于您提供的代码不能完全重现,因此不能保证下面的代码能够运行。它旨在作为如何构建实际解决方案的指南。如果您提供其他人可以用来运行您的代码的示例数据,您将得到更好的答案。

最简单的方法可能是将 net 拆分为一个列表,其中每个元素对应于 community 的每个唯一值,然后将图形构建代码应用于每个部分,存储另一个列表中每件作品的结果。在 R 中,有多种方法可以完成此类操作,其中之一是使用 lapply

#Break net into pieces based on unique values of community
netSplit <- split(net,net$community)

#Define a function to apply to each element of netSplit
myFun <- function(dataPiece){
    netEdges <- NULL

    for (idi in c("nom1", "nom2", "nom3")) {
        netEdge <- dataPiece[c("id", idi)]
        names(netEdge) <- c("id", "friendID")
        netEdge$weight <- 1
        netEdges <- rbind(netEdges, netEdge)
    }

    g <- graph.data.frame(netEdges, directed=TRUE)
    #This will return the graph itself; you could change the function
    # to return other values calculated on the graph
    g
}

#Apply your function to each subset (piece) of your data:
result <- lapply(netSplit,FUN = myFun)

如果一切顺利,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 of community 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 use lapply:

#Break net into pieces based on unique values of community
netSplit <- split(net,net$community)

#Define a function to apply to each element of netSplit
myFun <- function(dataPiece){
    netEdges <- NULL

    for (idi in c("nom1", "nom2", "nom3")) {
        netEdge <- dataPiece[c("id", idi)]
        names(netEdge) <- c("id", "friendID")
        netEdge$weight <- 1
        netEdges <- rbind(netEdges, netEdge)
    }

    g <- graph.data.frame(netEdges, directed=TRUE)
    #This will return the graph itself; you could change the function
    # to return other values calculated on the graph
    g
}

#Apply your function to each subset (piece) of your data:
result <- lapply(netSplit,FUN = myFun)

If all has gone well, result should be a list containing a graph (or whatever you modified myFun to return) for each unique value of community. Other popular tools for doing similar tasks include ddply from the plyr package.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文