challenge_1
Ananya Pujary
Loading Data and Creating a Network
Author

Ananya Pujary

Published

February 22, 2023

Challenge Overview

Today’s challenge is to

  1. read in a dataset, and

  2. create a network object

Load the Data

Reading in the got_marriages.csv file:

Code
got_marriages<-read_csv("../posts/_data/got/got_marriages.csv")
Rows: 255 Columns: 5
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (5): From, To, Type, Notes, Generation

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Code
got_marriages
# A tibble: 255 × 5
   From      To        Type    Notes  Generation
   <chr>     <chr>     <chr>   <chr>  <chr>     
 1 Targaryen Stark     Married R+L=J  Current   
 2 Baratheon Martell   Engaged died   Current   
 3 Baratheon Stark     Engaged broken Current   
 4 Martell   Essos     Married <NA>   Current   
 5 Martell   Reach     Affair  <NA>   Current   
 6 Martell   Essos     Affair  <NA>   Current   
 7 Martell   Essos     Affair  <NA>   Current   
 8 Martell   Septa     Affair  <NA>   Current   
 9 Martell   Dorne     Affair  <NA>   Current   
10 Martell   Targaryen Married <NA>   Current   
# … with 245 more rows

Create a Network

Creating an igraph object and finding out whether the network directed, weighted or bipartite.

Code
# not adjacency matrix, not n x n matrix => undirected
got_marriages.ig <-graph_from_data_frame(got_marriages, directed = FALSE)

head(got_marriages.ig)
6 x 20 sparse Matrix of class "dgCMatrix"
   [[ suppressing 20 column names 'Targaryen', 'Baratheon', 'Martell' ... ]]
                                                     
Targaryen 19 2 3 . .  4 . 7  1 1 2 3 . . 11 3 8 . 2 .
Baratheon  2 . 1 1 4  2 . 1  1 1 1 . . .  2 1 . . . .
Martell    3 1 . . .  1 . .  . . . . . .  . . 4 1 2 .
Lannister  . 1 . 4 .  1 . . 15 1 . . . 2  . . . . . .
Tyrell     . 4 . . .  8 . .  . . . . . .  . . . . . .
Reach      4 2 1 1 8 12 1 3  1 . . . . 2  . 1 1 . . .
Code
# describing the network
is_directed(got_marriages.ig)
[1] FALSE
Code
is_bipartite(got_marriages.ig)
[1] FALSE
Code
is_weighted(got_marriages.ig)
[1] FALSE

Thus, this network is undirected, unimodal, and unweighted.

Now, I’ll plot the network:

Code
# plotting the network 
plot(got_marriages.ig, edge.curved=.1, vertex.color = "orange")