0
我有一個R這樣的數據幀df。我想循環通過DF根據hee_provn1的不同值R:遍歷一列數據幀並輸出
npi_one npi_two hee_provn1
1 2 175221
3 4 175221
5 6 175221
7 8 175221
9 10 576546
11 12 576546
13 14 576546
15 16 789535
17 18 789535
19 20 789535
現在我的R代碼裏面是:
library(dplyr)
library(igraph)
df2 <- filter(df, hee_provn1 == '175221')
df3 <- df2 [,c("npi_one","npi_two")]
l = c(apply(df3,1,c))
G <- graph(l,directed = FALSE)
degree(G) -> d
closeness(G) -> c
betweenness(G) -> b
eigen_centrality(G)$vector -> e
cent_df = data.frame(d,c,b,e)
colnames(cent_df) <- c('degree', 'closeness','betweenness','eigen')
cbind(hee_provn1 = 175221,cent_df)
第一環路的結果表cent_df(hee_provn1 = 175221)是
hee_provn1 degree closeness betweenness eigen
1 175221 1 0.02040816 0 0.3227867
2 175221 1 0.02040816 0 0.3227867
3 175221 1 0.02040816 0 0.0000000
4 175221 1 0.02040816 0 0.0000000
5 175221 1 0.02040816 0 1.0000000
6 175221 1 0.02040816 0 1.0000000
7 175221 1 0.02040816 0 0.0000000
8 175221 1 0.02040816 0 0.0000000
第二環(hee_provn1 = 576546)的結果表cent_df是
hee_provn1 degree closeness betweenness eigen
1 576546 0 0.005494505 0 0
2 576546 0 0.005494505 0 0
3 576546 0 0.005494505 0 0
4 576546 0 0.005494505 0 0
5 576546 0 0.005494505 0 0
6 576546 0 0.005494505 0 0
7 576546 0 0.005494505 0 0
8 576546 0 0.005494505 0 0
9 576546 1 0.005917160 0 1
10 576546 1 0.005917160 0 1
11 576546 1 0.005917160 0 0
12 576546 1 0.005917160 0 0
13 576546 1 0.005917160 0 0
14 576546 1 0.005917160 0 0
我的想法結果槽形一個循環,我可以把所有的結果表一起在一個大的表像
hee_provn1 degree closeness betweenness eigen
1 175221 1 0.02040816 0 0.3227867
2 175221 1 0.02040816 0 0.3227867
3 175221 1 0.02040816 0 0.0000000
4 175221 1 0.02040816 0 0.0000000
5 175221 1 0.02040816 0 1.0000000
6 175221 1 0.02040816 0 1.0000000
7 175221 1 0.02040816 0 0.0000000
8 175221 1 0.02040816 0 0.0000000
9 576546 0 0.005494505 0 0
10 576546 0 0.005494505 0 0
11 576546 0 0.005494505 0 0
12 576546 0 0.005494505 0 0
13 576546 0 0.005494505 0 0
14 576546 0 0.005494505 0 0
15 576546 0 0.005494505 0 0
16 576546 0 0.005494505 0 0
17 576546 1 0.005917160 0 1
18 576546 1 0.005917160 0 1
19 576546 1 0.005917160 0 0
20 576546 1 0.005917160 0 0
21 576546 1 0.005917160 0 0
22 576546 1 0.005917160 0 0
我真的希望能儘可能高效。
什麼包是'graph'而來? – CPak
igraph。謝謝 – kkjoe
我已經編輯了包信息內的代碼,謝謝 – kkjoe