1

別の変数によって決定された各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ます。助けてくれて本当にありがとうございます!

4

1 に答える 1

1

提供するコードは完全に再現可能ではないため、以下のコードの実行は保証されません。これは、実際のソリューションを構築する方法のガイドとして意図されています。他の人があなたのコードを実行するために使用できるサンプルデータを提供すると、より良い答えが得られます。

これを行う最も簡単な方法は、おそらく、netの一意の値ごとに1つの要素を含むリストに分割し、communityグラフ作成コードを各ピースに適用して、各ピースの結果を別のリストに保存することです。Rでこのタイプのことを行うには、いくつかの方法があります。そのうちの1つは、次を使用すること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グラフ(または返すように変更したもの)を含むリストである必要があります。同様のタスクを実行するための他の一般的なツールには、パッケージからのものがあります。myFuncommunityddplyplyr

于 2011-07-27T00:50:31.720 に答える