User Tools

Site Tools


r:social_network_analysis_tutorial

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
r:social_network_analysis_tutorial [2019/11/27 07:29] – [Dataset] hkimscilr:social_network_analysis_tutorial [2023/11/23 08:44] – [Edges] hkimscil
Line 1: Line 1:
 ====== T1. ====== ====== T1. ======
 +''%%install.packages("igraph")%%''
 ===== Dataset ===== ===== Dataset =====
 <file csv star-wars-network-edges.csv> <file csv star-wars-network-edges.csv>
Line 91: Line 92:
 </file> </file>
 ===== Analysis ===== ===== Analysis =====
 +<code>
 +t.e <- read.csv("http://commres.net/wiki/_export/code/r/social_network_analysis_tutorial?codeblock=0", sep = ",")
 +head(t.e)
 +t.n <- read.csv("http://commres.net/wiki/_export/code/r/social_network_analysis_tutorial?codeblock=1", sep = ",")
 +head(t.n)
 +</code> 
  
 <code> <code>
Line 105: Line 112:
 > t.n <- read.csv("http://commres.net/wiki/_export/code/r/social_network_analysis_tutorial?codeblock=1", sep = ",") > t.n <- read.csv("http://commres.net/wiki/_export/code/r/social_network_analysis_tutorial?codeblock=1", sep = ",")
 > head(t.n) > head(t.n)
 +         name id
 +1       R2-D2  0
 +2   CHEWBACCA  1
 +3       C-3PO  2
 +4        LUKE  3
 +5 DARTH VADER  4
 +6       CAMIE  5
 +
 </code>  </code> 
  
Line 146: Line 161:
 id(v/n): id = vertices(nodes) graph, numeric id(v/n): id = vertices(nodes) graph, numeric
 weight(e/n): weight = edge graph, numeric weight(e/n): weight = edge graph, numeric
 +
 +<code>
 +V(g)        # print nodes
 +</code>
  
 <code> <code>
Line 185: Line 204:
  
 <code> <code>
-E(g) # edges+E(g) # print edges
 </code> </code>
  
Line 237: Line 256:
 > g[] # adjacency matrix > g[] # adjacency matrix
 22 x 22 sparse Matrix of class "dgCMatrix" 22 x 22 sparse Matrix of class "dgCMatrix"
-   [[ suppressing 22 column names ‘R2-D2’, ‘CHEWBACCA’, ‘C-3PO’ ... ]] +   [[ suppressing 22 column names ‘R2-D2’, ‘CHEWBACCA’, ‘C-3PO’ ... ]] ## 컬럼 id 생략
-                                                              +
 R2-D2        .  3 17 13 . . .  5 . .  6 . .  5 . . 1 . . . . . R2-D2        .  3 17 13 . . .  5 . .  6 . .  5 . . 1 . . . . .
 CHEWBACCA    3  .  5 16 1 . . 11 . .  7 . . 19 . . 1 . . . . . CHEWBACCA    3  .  5 16 1 . . 11 . .  7 . . 19 . . 1 . . . . .
Line 262: Line 280:
 GOLD FIVE    .  .  .  . . . .  . . .  . . .  . . . . . . . . . GOLD FIVE    .  .  .  . . . .  . . .  . . .  . . . . . . . . .
 </code> </code>
 +
 +<code>
 +g[1,] # first row of adjacency matrix
 +data.frame(g[1,]) # easy on eyes
 +</code>
 +
 +<code>
 +> g[1,] # first row of adjacency matrix
 +      R2-D2   CHEWBACCA       C-3PO        LUKE DARTH VADER       CAMIE       BIGGS        LEIA 
 +          0                    17          13                                         
 +       BERU        OWEN     OBI-WAN       MOTTI      TARKIN         HAN      GREEDO       JABBA 
 +          0                                                                       
 +    DODONNA GOLD LEADER       WEDGE  RED LEADER     RED TEN   GOLD FIVE 
 +          1                                                   
 +
 +> data.frame(g[1,])
 +            g.1...
 +R2-D2            0
 +CHEWBACCA        3
 +C-3PO           17
 +LUKE            13
 +DARTH VADER      0
 +CAMIE            0
 +BIGGS            0
 +LEIA             5
 +BERU             0
 +OWEN             0
 +OBI-WAN          6
 +MOTTI            0
 +TARKIN           0
 +HAN              5
 +GREEDO           0
 +JABBA            0
 +DODONNA          1
 +GOLD LEADER      0
 +WEDGE            0
 +RED LEADER       0
 +RED TEN          0
 +GOLD FIVE        0
 +</code>
 +===== Vis =====
 +<code>
 +plot(g)
 +</code>
 +{{:r:pasted:20191127-070843.png}}
 +
 +<code>
 +par(mar=c(0,0,0,0))
 +plot(g)
 +</code>
 +{{:r:pasted:20191127-070942.png}}
 +
 +
 +<code>
 +par(mar=c(0,0,0,0))
 +plot(g,
 +     vertex.color = "grey", # change color of nodes
 +     vertex.label.color = "black", # change color of labels
 +     vertex.label.cex = .75, # change size of labels to 75% of original size
 +     edge.curved=.15, # add a 15% curve to the edges
 +     edge.color="grey20") # change edge color to grey
 +</code>
 +{{:r:pasted:20191127-071117.png}}
 +
 +<code>
 +strength(g)
 +as.data.frame(strength(g))
 +</code>
 +
 +<code>
 +> strength(g)
 +      R2-D2   CHEWBACCA       C-3PO        LUKE DARTH VADER       CAMIE       BIGGS 
 +         50          63          64         129          11                    14 
 +       LEIA        BERU        OWEN     OBI-WAN       MOTTI      TARKIN         HAN 
 +         59                              49                    10          80 
 +     GREEDO       JABBA     DODONNA GOLD LEADER       WEDGE  RED LEADER     RED TEN 
 +          1                                                  13           
 +  GOLD FIVE 
 +          0 
 +> data.frame(strength(g))
 +            strength.g.
 +R2-D2                50
 +CHEWBACCA            63
 +C-3PO                64
 +LUKE                129
 +DARTH VADER          11
 +CAMIE                 4
 +BIGGS                14
 +LEIA                 59
 +BERU                  9
 +OWEN                  8
 +OBI-WAN              49
 +MOTTI                 4
 +TARKIN               10
 +HAN                  80
 +GREEDO                1
 +JABBA                 1
 +DODONNA               5
 +GOLD LEADER           5
 +WEDGE                 9
 +RED LEADER           13
 +RED TEN               2
 +GOLD FIVE             0
 +
 +</code>
 +
 +<code>
 +V(g)$size <- strength(g)
 +par(mar=c(0,0,0,0))
 +plot(g)
 +</code>
 +{{:r:pasted:20191127-071754.png}}
 +
 +
 +<code>
 +# taking the log to improve it
 +V(g)$size <- log(strength(g)) * 4 + 3
 +par(mar=c(0,0,0,0))
 +plot(g)
 +</code>
 +{{:r:pasted:20191127-071937.png}}
 +
 +<code>
 +plot(g,
 +     vertex.color = "grey", # change color of nodes
 +     vertex.label.color = "black", # change color of labels
 +     vertex.label.cex = .75, # change size of labels to 75% of original size
 +     edge.curved=.25, # add a 25% curve to the edges
 +     edge.color="grey20") # change edge color to grey
 +</code>
 +{{:r:pasted:20191127-072110.png}}
 +
 +<code>
 +V(g)$label <- ifelse( strength(g)>=10, V(g)$name, NA )
 +par(mar=c(0,0,0,0))
 +plot(g, 
 +     vertex.color = "grey", # change color of nodes
 +     vertex.label.color = "black", # change color of labels
 +     vertex.label.cex = .75, # change size of labels to 75% of original size
 +     edge.curved=.25, # add a 25% curve to the edges
 +     edge.color="grey20") # change edge color to grey
 +</code>
 +{{:r:pasted:20191127-072258.png}}
 +
 +<code>
 +t.n
 +</code>
 +
 +===== Using ifelse =====
 +<code>
 +t.n
 +</code>
 +
 +<code>
 +> t.n
 +          name id
 +1        R2-D2  0
 +2    CHEWBACCA  1
 +3        C-3PO  2
 +4         LUKE  3
 +5  DARTH VADER  4
 +6        CAMIE  5
 +7        BIGGS  6
 +8         LEIA  7
 +9         BERU  8
 +10        OWEN  9
 +11     OBI-WAN 10
 +12       MOTTI 11
 +13      TARKIN 12
 +14         HAN 13
 +15      GREEDO 14
 +16       JABBA 15
 +17     DODONNA 16
 +18 GOLD LEADER 17
 +19       WEDGE 18
 +20  RED LEADER 19
 +21     RED TEN 20
 +22   GOLD FIVE 21
 +
 +</code>
 +
 +<code>
 +t.n$name=="R2-D2"
 +</code>
 +
 +<code>
 +> t.n$name=="R2-D2"
 + [1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 +[13] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
 +
 +</code>
 +
 +<code>
 +as.data.frame(t.n$name=="R2-D2")
 +</code>
 +
 +<code>
 +> as.data.frame(t.n$name=="R2-D2")
 +   t.n$name == "R2-D2"
 +1                 TRUE
 +2                FALSE
 +3                FALSE
 +4                FALSE
 +5                FALSE
 +6                FALSE
 +7                FALSE
 +8                FALSE
 +9                FALSE
 +10               FALSE
 +11               FALSE
 +12               FALSE
 +13               FALSE
 +14               FALSE
 +15               FALSE
 +16               FALSE
 +17               FALSE
 +18               FALSE
 +19               FALSE
 +20               FALSE
 +21               FALSE
 +22               FALSE
 +
 +</code>
 +
 +
 +<code>
 +tmp.a <- as.data.frame(t.n)
 +tmp.b <- as.data.frame(t.n$name=="R2-D2")
 +tmp <- data.frame(tmp.a, tmp.b)
 +tmp
 +</code>
 +
 +<code>
 +> tmp.a <- as.data.frame(t.n)
 +> tmp.b <- as.data.frame(t.n$name=="R2-D2")
 +> tmp <- data.frame(tmp.a, tmp.b)
 +> tmp
 +          name id t.n.name.....R2.D2.
 +1        R2-D2  0                TRUE
 +2    CHEWBACCA  1               FALSE
 +3        C-3PO  2               FALSE
 +4         LUKE  3               FALSE
 +5  DARTH VADER  4               FALSE
 +6        CAMIE  5               FALSE
 +7        BIGGS  6               FALSE
 +8         LEIA  7               FALSE
 +9         BERU  8               FALSE
 +10        OWEN  9               FALSE
 +11     OBI-WAN 10               FALSE
 +12       MOTTI 11               FALSE
 +13      TARKIN 12               FALSE
 +14         HAN 13               FALSE
 +15      GREEDO 14               FALSE
 +16       JABBA 15               FALSE
 +17     DODONNA 16               FALSE
 +18 GOLD LEADER 17               FALSE
 +19       WEDGE 18               FALSE
 +20  RED LEADER 19               FALSE
 +21     RED TEN 20               FALSE
 +22   GOLD FIVE 21               FALSE
 +</code>
 +
 +
 +<code>
 +ifelse(t.n$name=="R2-D2", "yes", "no")
 +</code>
 +
 +<code>
 +> ifelse(t.n$name=="R2-D2", "yes", "no")
 + [1] "yes" "no"  "no"  "no"  "no"  "no"  "no"  "no"  "no"  "no"  "no"  "no" 
 +[13] "no"  "no"  "no"  "no"  "no"  "no"  "no"  "no"  "no"  "no" 
 +
 +</code>
 +
 +<code>
 +ifelse(grepl("R", t.n$name), "yes", "no") # grep function 
 +</code>
 +
 +<code>
 +> ifelse(grepl("R", t.n$name), "yes", "no")
 + [1] "yes" "no"  "no"  "no"  "yes" "no"  "no"  "no"  "yes" "no"  "no"  "no" 
 +[13] "yes" "no"  "yes" "no"  "no"  "yes" "no"  "yes" "yes" "no" 
 +
 +</code>
 +
 +<code>
 +tmp.a <- data.frame(t.n$name)
 +tmp.b <- data.frame(ifelse(grepl("R", t.n$name), "yes", "no"))
 +data.frame(tmp.a, tmp.b)
 +</code>
 +
 +<code>
 +> tmp.a <- data.frame(t.n$name)
 +> tmp.b <- data.frame(ifelse(grepl("R", t.n$name), "yes", "no"))
 +> data.frame(tmp.a, tmp.b)
 +      t.n.name ifelse.grepl..R...t.n.name....yes....no..
 +1        R2-D2                                       yes
 +2    CHEWBACCA                                        no
 +3        C-3PO                                        no
 +4         LUKE                                        no
 +5  DARTH VADER                                       yes
 +6        CAMIE                                        no
 +7        BIGGS                                        no
 +8         LEIA                                        no
 +9         BERU                                       yes
 +10        OWEN                                        no
 +11     OBI-WAN                                        no
 +12       MOTTI                                        no
 +13      TARKIN                                       yes
 +14         HAN                                        no
 +15      GREEDO                                       yes
 +16       JABBA                                        no
 +17     DODONNA                                        no
 +18 GOLD LEADER                                       yes
 +19       WEDGE                                        no
 +20  RED LEADER                                       yes
 +21     RED TEN                                       yes
 +22   GOLD FIVE                                        no
 +
 +</code>
 +===== Coloring nodes =====
 +<code>
 +dark_side <- c("DARTH VADER", "MOTTI", "TARKIN")
 +light_side <- c("R2-D2", "CHEWBACCA", "C-3PO", "LUKE", "CAMIE", "BIGGS",
 +                "LEIA", "BERU", "OWEN", "OBI-WAN", "HAN", "DODONNA",
 +                "GOLD LEADER", "WEDGE", "RED LEADER", "RED TEN", "GOLD FIVE")
 +other <- c("GREEDO", "JABBA")
 +# node we'll create a new color variable as a node property
 +V(g)$color <- NA
 +V(g)$color[V(g)$name %in% dark_side] <- "red"
 +V(g)$color[V(g)$name %in% light_side] <- "gold"
 +V(g)$color[V(g)$name %in% other] <- "grey20"
 +vertex_attr(g)
 +</code>
 +
 +<code>
 +> dark_side <- c("DARTH VADER", "MOTTI", "TARKIN")
 +> light_side <- c("R2-D2", "CHEWBACCA", "C-3PO", "LUKE", "CAMIE", "BIGGS",
 ++                 "LEIA", "BERU", "OWEN", "OBI-WAN", "HAN", "DODONNA",
 ++                 "GOLD LEADER", "WEDGE", "RED LEADER", "RED TEN", "GOLD FIVE")
 +> other <- c("GREEDO", "JABBA")
 +> # node we'll create a new color variable as a node property
 +> V(g)$color <- NA
 +> V(g)$color[V(g)$name %in% dark_side] <- "red"
 +> V(g)$color[V(g)$name %in% light_side] <- "gold"
 +> V(g)$color[V(g)$name %in% other] <- "grey20"
 +> vertex_attr(g)
 +$name
 + [1] "R2-D2"       "CHEWBACCA"   "C-3PO"       "LUKE"        "DARTH VADER"
 + [6] "CAMIE"       "BIGGS"       "LEIA"        "BERU"        "OWEN"       
 +[11] "OBI-WAN"     "MOTTI"       "TARKIN"      "HAN"         "GREEDO"     
 +[16] "JABBA"       "DODONNA"     "GOLD LEADER" "WEDGE"       "RED LEADER" 
 +[21] "RED TEN"     "GOLD FIVE"  
 +
 +$id
 + [1]  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21
 +
 +$size
 + [1] 18.648092 19.572539 19.635532 22.439250 12.591581  8.545177 13.556229
 + [8] 19.310150 11.788898 11.317766 18.567281  8.545177 12.210340 20.528107
 +[15]  3.000000  3.000000  9.437752  9.437752 11.788898 13.259797  5.772589
 +[22]      -Inf
 +
 +$label
 + [1] "R2-D2"       "CHEWBACCA"   "C-3PO"       "LUKE"        "DARTH VADER"
 + [6] NA            "BIGGS"       "LEIA"        NA            NA           
 +[11] "OBI-WAN"     NA            "TARKIN"      "HAN"         NA           
 +[16] NA            NA            NA            NA            "RED LEADER" 
 +[21] NA            NA           
 +
 +$color
 + [1] "gold"   "gold"   "gold"   "gold"   "red"    "gold"   "gold"   "gold"  
 + [9] "gold"   "gold"   "gold"   "red"    "red"    "gold"   "grey20" "grey20"
 +[17] "gold"   "gold"   "gold"   "gold"   "gold"   "gold"  
 +
 +
 +</code>
 +
 +<code>
 +par(mar=c(0,0,0,0))
 +plot(g)
 +</code>
 +{{:r:pasted:20191127-074438.png}}
 +
 +===== %in% =====
 +
 +<code>
 +# what does %in% do?
 +1 %in% c(1,2,3,4)
 +1 %in% c(2,3,4)
 +</code>
 +
 +<code>
 +> 1 %in% c(1,2,3,4)
 +[1] TRUE
 +> 1 %in% c(2,3,4)
 +[1] FALSE
 +
 +</code>
 +
 +<code>
 +dark_side <- c("DARTH VADER", "MOTTI", "TARKIN")
 +light_side <- c("R2-D2", "CHEWBACCA", "C-3PO", "LUKE", "CAMIE", "BIGGS",
 +                "LEIA", "BERU", "OWEN", "OBI-WAN", "HAN", "DODONNA",
 +                "GOLD LEADER", "WEDGE", "RED LEADER", "RED TEN", "GOLD FIVE")
 +other <- c("GREEDO", "JABBA")
 +# node we'll create a new color variable as a node property
 +V(g)$color <- NA # V(g)에 color라는 컬럼을 만든다
 +V(g)$color[V(g)$name %in% dark_side] <- "red" 
 +# V(g)$name이 dark_side에 있는 이름이면 color 컬럼에 "red"를 적는다.
 +V(g)$color[V(g)$name %in% light_side] <- "gold"
 +V(g)$color[V(g)$name %in% other] <- "grey20"
 +vertex_attr(g)
 +</code>
 +
 +<code>
 +par(mar=c(0,0,0,0))
 +plot(g)
 +legend(x=.75, y=.75, legend=c("Dark side", "Light side", "Other"), 
 +       pch=21, pt.bg=c("red", "gold", "grey20"), pt.cex=2, bty="n")
 +</code>
 +{{:r:pasted:20191127-075029.png}}
 +
 +===== Edges =====
 +<code>
 +E(g)$width <- log(E(g)$weight) + 1.5 # create width column in the graph, g
 +edge_attr(g)
 +</code>
 +<code>
 +> E(g)$width <- log(E(g)$weight) + 1.5 # create width column in the graph, g
 +> edge_attr(g)
 +$weight
 + [1] 17 13  6  5  5  3  1  7  5 16 19 11  1  1  2  2
 +[17]  4  1  3  3  2  3 18  2  6 17  1 19  6  1  2  1
 +[33]  7  9 26  1  1  6  1  1 13  1  1  1  1  1  1  2
 +[49]  1  1  3  3  1  1  3  1  2  1  1  1
 +
 +$width
 + [1] 4.333213 4.064949 3.291759 3.109438 3.109438
 + [6] 2.598612 1.500000 3.445910 3.109438 4.272589
 +[11] 4.444439 3.897895 1.500000 1.500000 2.193147
 +[16] 2.193147 2.886294 1.500000 2.598612 2.598612
 +[21] 2.193147 2.598612 4.390372 2.193147 3.291759
 +[26] 4.333213 1.500000 4.444439 3.291759 1.500000
 +[31] 2.193147 1.500000 3.445910 3.697225 4.758097
 +[36] 1.500000 1.500000 3.291759 1.500000 1.500000
 +[41] 4.064949 1.500000 1.500000 1.500000 1.500000
 +[46] 1.500000 1.500000 2.193147 1.500000 1.500000
 +[51] 2.598612 2.598612 1.500000 1.500000 2.598612
 +[56] 1.500000 2.193147 1.500000 1.500000 1.500000
 +</code>
 +
 +<code>
 +par(mar=c(0,0,0,0))
 +plot(g)
 +</code>
 +{{:r:pasted:20191127-080219.png}}
 +
 +<code>
 +par(mfrow=c(2, 3), mar=c(0,0,1,0))
 +plot(g, layout=layout_randomly, main="Random")
 +plot(g, layout=layout_in_circle, main="Circle")
 +plot(g, layout=layout_as_star, main="Star")
 +plot(g, layout=layout_as_tree, main="Tree")
 +plot(g, layout=layout_on_grid, main="Grid")
 +plot(g, layout=layout_with_fr, main="Force-directed")
 +par(mfrow=c(1, 1), mar=c(0,0,0,0)) # set it back to the previous setting
 +</code>
 +{{:r:pasted:20191127-080437.png}}
 +
 +cf. layout_with_fr: The Fruchterman-Reingold layout algorithm see [[https://www.rdocumentation.org/packages/igraph/versions/1.3.5/topics/layout_with_fr]] page
 +<code>
 +par(mfrow=c(1,2))
 +set.seed(777)
 +fr <- layout_with_fr(g, niter=1000)
 +par(mar=c(0,0,0,0)); plot(g, layout=fr)
 +set.seed(666)
 +fr <- layout_with_fr(g, niter=1000)
 +par(mar=c(0,0,0,0)); plot(g, layout=fr)
 +
 +</code>
 +{{:r:pasted:20191127-080758.png}}
 +
 +
 +===== Measurements =====
 +<code>
 +degree(g, mode = "all")
 +closeness(g, mode = "all", weights = NA, normalized = T)
 +betweenness(g, directed = F, weights = NA, normalized = T)
 +</code>
 +
 +<code>
 +> degree(g, mode = "all")
 +      R2-D2   CHEWBACCA       C-3PO        LUKE DARTH VADER       CAMIE       BIGGS 
 +          7                    10          15                               
 +       LEIA        BERU        OWEN     OBI-WAN       MOTTI      TARKIN         HAN 
 +         12                                                             
 +     GREEDO       JABBA     DODONNA GOLD LEADER       WEDGE  RED LEADER     RED TEN 
 +          1                                                             
 +  GOLD FIVE 
 +          0 
 +> closeness(g, mode = "all", weights = NA, normalized = T)
 +      R2-D2   CHEWBACCA       C-3PO        LUKE DARTH VADER       CAMIE       BIGGS 
 + 0.38181818  0.38888889  0.40384615  0.44680851  0.32812500  0.32307692  0.36842105 
 +       LEIA        BERU        OWEN     OBI-WAN       MOTTI      TARKIN         HAN 
 + 0.42000000  0.35000000  0.32812500  0.38181818  0.31343284  0.31343284  0.38888889 
 +     GREEDO       JABBA     DODONNA GOLD LEADER       WEDGE  RED LEADER     RED TEN 
 + 0.28767123  0.28767123  0.34426230  0.33870968  0.33870968  0.36842105  0.32307692 
 +  GOLD FIVE 
 + 0.04545455 
 +Warning message:
 +In closeness(g, mode = "all", weights = NA, normalized = T) :
 +  At centrality.c:2784 :closeness centrality is not well-defined for disconnected graphs
 +> betweenness(g, directed = F, weights = NA, normalized = T)
 +      R2-D2   CHEWBACCA       C-3PO        LUKE DARTH VADER       CAMIE       BIGGS 
 +0.011904762 0.035487528 0.057596372 0.297278912 0.011904762 0.000000000 0.032142857 
 +       LEIA        BERU        OWEN     OBI-WAN       MOTTI      TARKIN         HAN 
 +0.217120181 0.005442177 0.000000000 0.014852608 0.000000000 0.000000000 0.176190476 
 +     GREEDO       JABBA     DODONNA GOLD LEADER       WEDGE  RED LEADER     RED TEN 
 +0.000000000 0.000000000 0.011111111 0.003174603 0.003174603 0.032142857 0.000000000 
 +  GOLD FIVE 
 +0.000000000 
 +
 +</code>
 +
 +<code>
 +g.c.deg <- degree(g, mode = "all")
 +g.c.clo <- closeness(g, mode = "all", weights = NA, normalized = T)
 +g.c.bet <- betweenness(g, directed = F, weights = NA, normalized = T)
 +tmp <- data.frame(g.c.deg, g.c.clo, g.c.bet)
 +tmp[order(-g.c.clo, -g.c.bet),]
 +</code>
 +
 +<code>
 +> g.c.deg <- degree(g, mode = "all")
 +> g.c.clo <- closeness(g, mode = "all", weights = NA, normalized = T)
 +Warning message:
 +In closeness(g, mode = "all", weights = NA, normalized = T) :
 +  At centrality.c:2784 :closeness centrality is not well-defined for disconnected graphs
 +> g.c.bet <- betweenness(g, directed = F, weights = NA, normalized = T)
 +> tmp <- data.frame(g.c.deg, g.c.clo, g.c.bet)
 +
 +> tmp
 +            g.c.deg    g.c.clo     g.c.bet
 +R2-D2             7 0.38181818 0.011904762
 +CHEWBACCA         8 0.38888889 0.035487528
 +C-3PO            10 0.40384615 0.057596372
 +LUKE             15 0.44680851 0.297278912
 +DARTH VADER       5 0.32812500 0.011904762
 +CAMIE             2 0.32307692 0.000000000
 +BIGGS             7 0.36842105 0.032142857
 +LEIA             12 0.42000000 0.217120181
 +BERU              4 0.35000000 0.005442177
 +OWEN              3 0.32812500 0.000000000
 +OBI-WAN           7 0.38181818 0.014852608
 +MOTTI             3 0.31343284 0.000000000
 +TARKIN            3 0.31343284 0.000000000
 +HAN               8 0.38888889 0.176190476
 +GREEDO            1 0.28767123 0.000000000
 +JABBA             1 0.28767123 0.000000000
 +DODONNA           5 0.34426230 0.011111111
 +GOLD LEADER       5 0.33870968 0.003174603
 +WEDGE             5 0.33870968 0.003174603
 +RED LEADER        7 0.36842105 0.032142857
 +RED TEN           2 0.32307692 0.000000000
 +GOLD FIVE         0 0.04545455 0.000000000
 +
 +> tmp[order(-g.c.clo, -g.c.bet),]
 +            g.c.deg    g.c.clo     g.c.bet
 +LUKE             15 0.44680851 0.297278912
 +LEIA             12 0.42000000 0.217120181
 +C-3PO            10 0.40384615 0.057596372
 +HAN               8 0.38888889 0.176190476
 +CHEWBACCA         8 0.38888889 0.035487528
 +OBI-WAN           7 0.38181818 0.014852608
 +R2-D2             7 0.38181818 0.011904762
 +BIGGS             7 0.36842105 0.032142857
 +RED LEADER        7 0.36842105 0.032142857
 +BERU              4 0.35000000 0.005442177
 +DODONNA           5 0.34426230 0.011111111
 +GOLD LEADER       5 0.33870968 0.003174603
 +WEDGE             5 0.33870968 0.003174603
 +DARTH VADER       5 0.32812500 0.011904762
 +OWEN              3 0.32812500 0.000000000
 +CAMIE             2 0.32307692 0.000000000
 +RED TEN           2 0.32307692 0.000000000
 +MOTTI             3 0.31343284 0.000000000
 +TARKIN            3 0.31343284 0.000000000
 +GREEDO            1 0.28767123 0.000000000
 +JABBA             1 0.28767123 0.000000000
 +GOLD FIVE         0 0.04545455 0.000000000
 +
 +</code>
 +
 +<code>
 +g.c.bac <- distances(g, v=V(g)["R2-D2"], to=V(g), weights=NA)
 +g.c.bac
 +</code>
 +
 +<code>
 +> g.c.bac <- distances(g, v=V(g)["R2-D2"], to=V(g), weights=NA)
 +> g.c.bac
 +      R2-D2 CHEWBACCA C-3PO LUKE DARTH VADER CAMIE BIGGS LEIA BERU OWEN OBI-WAN MOTTI
 +R2-D2                    1                      1    2    2           2
 +      TARKIN HAN GREEDO JABBA DODONNA GOLD LEADER WEDGE RED LEADER RED TEN GOLD FIVE
 +R2-D2      2        2                                  2             Inf
 +</code>
 +
r/social_network_analysis_tutorial.txt · Last modified: 2023/11/23 08:50 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki