Code
library(readr)
Warning: package 'readr' was built under R version 4.1.3
Yakub Rabiutheen
February 27, 2023
Today’s challenge is to
read in a dataset, and
create a network object
Read in one (or more) of the following data sets, using the correct R package and command.
tidyr
package)Find the _data
folder, located inside the posts
folder. Then you can read in the data, using base read.csv
or read_csv
standard tidy read command to read Comma Separated Values files or, alternatively, read the data in directly from loading a package.
Rows: 255 Columns: 5
-- Column specification --------------------------------------------------------
Delimiter: ","
chr (5): From, To, Type, Notes, Generation
i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.
Add any comments or documentation as needed. More challenging data sets may require additional code chunks and documentation.
# A tibble: 255 x 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
Load the package igraph
and create an igraph
object (i.e. a graph or network) in the form of an edge list. The command may vary whether the data is given as a list of connections or an adjacency matrix. Is the network directed or undirected; weighted or unweighted; unimodal or bipartite? Can you plot it?
Warning: package 'igraph' was built under R version 4.1.3
Attaching package: 'igraph'
The following objects are masked from 'package:stats':
decompose, spectrum
The following object is masked from 'package:base':
union
IGRAPH 76aa63c UN-- 20 255 --
+ attr: name (v/c), Type (e/c), Notes (e/c), Generation (e/c)
+ edges from 76aa63c (vertex names):
[1] Targaryen--Stark Baratheon--Martell Baratheon--Stark
[4] Martell --Essos Martell --Reach Martell --Essos
[7] Martell --Essos Martell --Septa Martell --Dorne
[10] Targaryen--Martell Targaryen--Essos Targaryen--Essos
[13] Baratheon--Lannister Baratheon--Vale Baratheon--Riverlands
[16] Baratheon--Crownlands Baratheon--Reach Baratheon--Westerlands
[19] Baratheon--Crownlands Lannister--Lannister Baratheon--Reach
[22] Baratheon--Tyrell Tyrell --Reach Tyrell --Reach
+ ... omitted several edges
##Node and Edge Exploration
+ 20/20 vertices, named, from 76aa63c:
[1] Targaryen Baratheon Martell Lannister Tyrell Reach
[7] North Riverlands Westerlands Stark Vale Arryn
[13] Tully Frey Crownlands Stormlands Essos Septa
[19] Dorne Beyond Wall
+ 255/255 edges from 76aa63c (vertex names):
[1] Targaryen--Stark Baratheon--Martell Baratheon--Stark
[4] Martell --Essos Martell --Reach Martell --Essos
[7] Martell --Essos Martell --Septa Martell --Dorne
[10] Targaryen--Martell Targaryen--Essos Targaryen--Essos
[13] Baratheon--Lannister Baratheon--Vale Baratheon--Riverlands
[16] Baratheon--Crownlands Baratheon--Reach Baratheon--Westerlands
[19] Baratheon--Crownlands Lannister--Lannister Baratheon--Reach
[22] Baratheon--Tyrell Tyrell --Reach Tyrell --Reach
[25] Tyrell --Reach Tyrell --Reach Tyrell --Reach
[28] Baratheon--Tyrell Tyrell --Reach Baratheon--Tyrell
+ ... omitted several edges
##Directed,Bipariate,Weighted Test
[1] FALSE
[1] FALSE
[1] FALSE
This network is not directed, it’s unweighted and unimodal.
---
title: "Week 1 Challenge Instructions"
author: "Yakub Rabiutheen"
description: "Loading Data and Creating a Network"
date: "02/27/2023"
format:
html:
toc: true
code-fold: true
code-copy: true
code-tools: true
# editor: visual
categories:
- challenge_1
- instructions
# - railroads
# - faostat
# - wildbirds
---
```{r}
library(readr)
```
## Challenge Overview
Today's challenge is to
1) read in a dataset, and
2) create a network object
## Load the Data
Read in one (or more) of the following data sets, using the correct R package and command.
- got_marriages.csv
- fish_encounters dataset (available in the `tidyr` package)
- got_like_dislike.csv
Find the `_data` folder, located inside the `posts` folder. Then you can read in the data, using base `read.csv` or `read_csv` standard tidy read command to read Comma Separated Values files or, alternatively, read the data in directly from loading a package.
```{r}
library(readr)
setwd("~/GitHub/Social_Networks_Spring_2023/posts/_data")
got_marriages <- read_csv("got/got_marriages.csv")
```
Add any comments or documentation as needed. More challenging data sets may require additional code chunks and documentation.
```{r}
got_marriages
```
## Create a Network
Load the package `igraph` and create an `igraph` object (i.e. a graph or network) in the form of an edge list. The command may vary whether the data is given as a list of connections or an adjacency matrix. Is the network directed or undirected; weighted or unweighted; unimodal or bipartite? Can you plot it?
```{r}
library(igraph)
got_marriages.ig = graph_from_data_frame(got_marriages, directed = FALSE)
got_marriages.ig
```
##Node and Edge Exploration
```{r}
##Nodes
V(got_marriages.ig)
```
```{r}
E(got_marriages.ig)
```
##Directed,Bipariate,Weighted Test
```{r}
is_bipartite(got_marriages.ig)
is_directed(got_marriages.ig)
is_weighted(got_marriages.ig)
```
This network is not directed, it's unweighted and unimodal.
##Plot The Network
```{r}
plot(got_marriages.ig)
```