2017-08-01 65 views
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 

我真的希望能儘可能高效。

+0

什麼包是'graph'而來? – CPak

+0

igraph。謝謝 – kkjoe

+0

我已經編輯了包信息內的代碼,謝謝 – kkjoe

回答

1

你的榜樣數據

df <- data.frame(npi_one=seq(1,19,2), 
       npi_two=seq(2,20,2), 
       hee_provn1=c(rep(175221,4),rep(576546,3),rep(789535,3))) 

除了igraph你需要tidyverse

library(tidyverse) 
library(igraph) 

我已經註釋下面的代碼以匹配您的原代碼

final <- df %>% 
     group_by(hee_provn1) %>%  # similar to filter(df, hee_provn1 == '175221') 
     nest() %>%     # similar to df2 [,c("npi_one","npi_two")] 
     mutate(data=map(data,~c(apply(.x,1,c)))) %>% # similar to c(apply(df3,1,c)) 
     mutate(data=map(data,~graph(.x,directed=F))) %>% # similar to graph(l,directed = FALSE) 
     mutate(data=map(data,~ data.frame(degree = degree(.x), 
              closeness = closeness(.x), 
              betweenness = betweenness(.x), 
              eigen_centrality = eigen_centrality(.x)$vector))) %>% # similar to making b, c, d, e individually 
     unnest(data) # revert to normal data frame 

輸出head(final)

hee_provn1 degree closeness betweenness eigen_centrality 
1  175221  1 0.020408163   0  1.000000e+00 
2  175221  1 0.020408163   0  1.000000e+00 
3  175221  1 0.020408163   0  0.000000e+00 
4  175221  1 0.020408163   0  0.000000e+00 

注意我每次運行eigen_centrality我得到不同的值,所以一定要確保它返回預期的值

+0

您可以通過接受答案來關閉此問題,左邊的複選標記 – CPak