challenge_1
instruction
Loading Data and Creating a Network
Author

Ben Ramsey

Published

February 13, 2023

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.

Code
library(tidyverse)
-- Attaching packages --------------------------------------- tidyverse 1.3.1 --
v ggplot2 3.3.5     v purrr   0.3.4
v tibble  3.1.5     v dplyr   1.0.7
v tidyr   1.1.4     v stringr 1.4.0
v readr   2.0.2     v forcats 0.5.1
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::filter() masks stats::filter()
x dplyr::lag()    masks stats::lag()
Code
got_marriages <- read_csv("_data/got/got_marriages.csv")
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.

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?

Code
library(igraph)

Attaching package: 'igraph'
The following objects are masked from 'package:dplyr':

    as_data_frame, groups, union
The following objects are masked from 'package:purrr':

    compose, simplify
The following object is masked from 'package:tidyr':

    crossing
The following object is masked from 'package:tibble':

    as_data_frame
The following objects are masked from 'package:stats':

    decompose, spectrum
The following object is masked from 'package:base':

    union
Code
got_marriages.ig <- graph_from_data_frame(got_marriages, directed = FALSE)

is_directed(got_marriages.ig)
[1] FALSE
Code
is_weighted(got_marriages.ig)
[1] FALSE
Code
is_bipartite(got_marriages.ig)
[1] FALSE
Code
plot.igraph(got_marriages.ig)