library(igraph)
load('data/examples_for_videos.RData') # loads the networks we will use
g_ud_uw
## IGRAPH 767ab4c UN-- 5 6 --
## + attr: name (v/c)
## + edges from 767ab4c (vertex names):
## [1] Alex --Beth Alex --Chris Alex --Doug Alex --Eliza Beth --Doug
## [6] Chris--Doug
am_ud_uw <- as_adjacency_matrix(graph = g_ud_uw)
am_ud_uw
## 5 x 5 sparse Matrix of class "dgCMatrix"
## Alex Beth Chris Doug Eliza
## Alex . 1 1 1 1
## Beth 1 . . 1 .
## Chris 1 . . 1 .
## Doug 1 1 1 . .
## Eliza 1 . . . .
g_ud_uw_am <- graph_from_adjacency_matrix(adjmatrix = am_ud_uw, mode = 'undirected', weighted = NULL)
g_ud_uw_am
## IGRAPH 8aac838 UN-- 5 6 --
## + attr: name (v/c)
## + edges from 8aac838 (vertex names):
## [1] Alex --Beth Alex --Chris Alex --Doug Alex --Eliza Beth --Doug
## [6] Chris--Doug
el_ud_uw <- as_edgelist(graph = g_ud_uw)
el_ud_uw
## [,1] [,2]
## [1,] "Alex" "Beth"
## [2,] "Alex" "Chris"
## [3,] "Alex" "Doug"
## [4,] "Alex" "Eliza"
## [5,] "Beth" "Doug"
## [6,] "Chris" "Doug"
g_ud_uw_el <- graph_from_edgelist(el = el_ud_uw, directed = FALSE)
g_ud_uw_el
## IGRAPH 6fcbb8f UN-- 5 6 --
## + attr: name (v/c)
## + edges from 6fcbb8f (vertex names):
## [1] Alex --Beth Alex --Chris Alex --Doug Alex --Eliza Beth --Doug
## [6] Chris--Doug
g_ud_w
## IGRAPH 767ab4c UNW- 5 6 --
## + attr: name (v/c), weight (e/n)
## + edges from 767ab4c (vertex names):
## [1] Alex --Beth Alex --Chris Alex --Doug Alex --Eliza Beth --Doug
## [6] Chris--Doug
am_ud_w <- as_adjacency_matrix(graph = g_ud_w, attr = 'weight')
am_ud_w
## 5 x 5 sparse Matrix of class "dgCMatrix"
## Alex Beth Chris Doug Eliza
## Alex . 1 4 1 2
## Beth 1 . . 5 .
## Chris 4 . . 3 .
## Doug 1 5 3 . .
## Eliza 2 . . . .
g_ud_w_am <- graph_from_adjacency_matrix(adjmatrix = am_ud_w, mode = 'undirected',
weighted = 'weight')
g_ud_w_am
## IGRAPH e738398 UNW- 5 6 --
## + attr: name (v/c), weight (e/n)
## + edges from e738398 (vertex names):
## [1] Alex --Beth Alex --Chris Alex --Doug Alex --Eliza Beth --Doug
## [6] Chris--Doug
el_ud_w <- as_edgelist(graph = g_ud_w)
el_ud_w # weights are missing!
## [,1] [,2]
## [1,] "Alex" "Beth"
## [2,] "Alex" "Chris"
## [3,] "Alex" "Doug"
## [4,] "Alex" "Eliza"
## [5,] "Beth" "Doug"
## [6,] "Chris" "Doug"
#first workaround
cbind(as_edgelist(graph = g_ud_w), E(g_ud_w)$weight)
## [,1] [,2] [,3]
## [1,] "Alex" "Beth" "1"
## [2,] "Alex" "Chris" "4"
## [3,] "Alex" "Doug" "1"
## [4,] "Alex" "Eliza" "2"
## [5,] "Beth" "Doug" "5"
## [6,] "Chris" "Doug" "3"
el_ud_w <- cbind(as_edgelist(graph = g_ud_w), E(g_ud_w)$weight) # for the next chunk
# second workaround
get.data.frame(x = g_ud_w, what = 'edges')
# we cannot use graph_from_edgelist to create a weighted network as there is no argument to specify weights
# data frame
el_ud_w <- as.data.frame(el_ud_w)
colnames(el_ud_w)[3] <- 'weight' # relabel the 3rd column as 'weight'
g_ud_uw_el <- graph_from_data_frame(d = el_ud_w, directed = FALSE, vertices = NULL)
g_ud_uw_el
## IGRAPH 5ce81d8 UNW- 5 6 --
## + attr: name (v/c), weight (e/c)
## + edges from 5ce81d8 (vertex names):
## [1] Alex --Beth Alex --Chris Alex --Doug Alex --Eliza Beth --Doug
## [6] Chris--Doug
g_d_uw
## IGRAPH 56acf83 DN-- 5 6 --
## + attr: name (v/c)
## + edges from 56acf83 (vertex names):
## [1] Alex ->Beth Alex ->Chris Alex ->Doug Alex ->Eliza Beth ->Doug
## [6] Chris->Doug
am_d_uw <- as_adjacency_matrix(graph = g_d_uw)
am_d_uw
## 5 x 5 sparse Matrix of class "dgCMatrix"
## Alex Beth Chris Doug Eliza
## Alex . 1 1 1 1
## Beth . . . 1 .
## Chris . . . 1 .
## Doug . . . . .
## Eliza . . . . .
g_d_uw_am <- graph_from_adjacency_matrix(adjmatrix = am_d_uw, mode = 'directed', weighted = NULL)
g_d_uw_am
## IGRAPH 14a3b55 DN-- 5 6 --
## + attr: name (v/c)
## + edges from 14a3b55 (vertex names):
## [1] Alex ->Beth Alex ->Chris Alex ->Doug Beth ->Doug Chris->Doug
## [6] Alex ->Eliza
el_d_uw <- as_edgelist(graph = g_ud_uw)
el_d_uw # this is not different from el_ud_uw! but your interpretation of the data is different.
## [,1] [,2]
## [1,] "Alex" "Beth"
## [2,] "Alex" "Chris"
## [3,] "Alex" "Doug"
## [4,] "Alex" "Eliza"
## [5,] "Beth" "Doug"
## [6,] "Chris" "Doug"
g_d_uw_el <- graph_from_edgelist(el = el_d_uw, directed = TRUE)
g_d_uw_el
## IGRAPH b73b15d DN-- 5 6 --
## + attr: name (v/c)
## + edges from b73b15d (vertex names):
## [1] Alex ->Beth Alex ->Chris Alex ->Doug Alex ->Eliza Beth ->Doug
## [6] Chris->Doug
g_d_w
## IGRAPH 56acf83 DNW- 5 6 --
## + attr: name (v/c), weight (e/n)
## + edges from 56acf83 (vertex names):
## [1] Alex ->Beth Alex ->Chris Alex ->Doug Alex ->Eliza Beth ->Doug
## [6] Chris->Doug
am_d_w <- as_adjacency_matrix(graph = g_d_w, attr = 'weight')
am_d_w
## 5 x 5 sparse Matrix of class "dgCMatrix"
## Alex Beth Chris Doug Eliza
## Alex . 5 1 5 1
## Beth . . . 4 .
## Chris . . . 5 .
## Doug . . . . .
## Eliza . . . . .
g_d_w_am <- graph_from_adjacency_matrix(adjmatrix = am_d_w, mode = 'directed', weighted = 'weight')
g_d_w_am
## IGRAPH c462bc7 DNW- 5 6 --
## + attr: name (v/c), weight (e/n)
## + edges from c462bc7 (vertex names):
## [1] Alex ->Beth Alex ->Chris Alex ->Doug Beth ->Doug Chris->Doug
## [6] Alex ->Eliza
el_d_w <- as_edgelist(graph = g_d_w)
el_d_w # we run into the same problem as before - the weights are missing
## [,1] [,2]
## [1,] "Alex" "Beth"
## [2,] "Alex" "Chris"
## [3,] "Alex" "Doug"
## [4,] "Alex" "Eliza"
## [5,] "Beth" "Doug"
## [6,] "Chris" "Doug"
#first workaround
cbind(as_edgelist(graph = g_d_w), E(g_d_w)$weight)
## [,1] [,2] [,3]
## [1,] "Alex" "Beth" "5"
## [2,] "Alex" "Chris" "1"
## [3,] "Alex" "Doug" "5"
## [4,] "Alex" "Eliza" "1"
## [5,] "Beth" "Doug" "4"
## [6,] "Chris" "Doug" "5"
el_d_w <- cbind(as_edgelist(graph = g_d_w), E(g_d_w)$weight) # for the next chunk
# second workaround
get.data.frame(x = g_d_w, what = 'edges')
# we cannot use graph_from_edgelist to create a weighted network as there is no argument to specify weights
# data frame
el_d_w <- as.data.frame(el_d_w)
colnames(el_d_w)[3] <- 'weight' # relabel the 3rd column as 'weight'
g_d_w_el <- graph_from_data_frame(d = el_d_w, directed = TRUE, vertices = NULL)
g_d_w_el
## IGRAPH 988a5c0 DNW- 5 6 --
## + attr: name (v/c), weight (e/c)
## + edges from 988a5c0 (vertex names):
## [1] Alex ->Beth Alex ->Chris Alex ->Doug Alex ->Eliza Beth ->Doug
## [6] Chris->Doug
Code chunk to generate the example networks.
g_ud_uw <- graph_from_literal(Alex-Beth,
Chris-Doug, Eliza-Alex,
Beth-Doug, Alex-Chris,
Doug-Alex)
g_ud_w <- g_ud_uw
set.seed(1)
E(g_ud_w)$weight <- sample(1:5, 6, replace = T)
g_d_uw <- as.directed(g_ud_uw, mode = 'arbitrary')
g_d_w <- g_d_uw
set.seed(2)
E(g_d_w)$weight <- sample(1:5, 6, replace = T)
save(g_ud_uw, g_ud_w, g_d_uw, g_d_w, file = 'examples_for_video1.RData')