提供するコードは完全に再現可能ではないため、以下のコードの実行は保証されません。これは、実際のソリューションを構築する方法のガイドとして意図されています。他の人があなたのコードを実行するために使用できるサンプルデータを提供すると、より良い答えが得られます。
これを行う最も簡単な方法は、おそらく、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
グラフ(または返すように変更したもの)を含むリストである必要があります。同様のタスクを実行するための他の一般的なツールには、パッケージからのものがあります。myFun
community
ddply
plyr