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
Code
library(network)
Warning: package 'network' was built under R version 4.1.3
'network' 1.18.1 (2023-01-24), part of the Statnet Project
* 'news(package="network")' for changes since last version
* 'citation("network")' for citation information
* 'https://statnet.org' for help, support, and other information
Attaching package: 'network'
The following objects are masked from 'package:igraph':
%c%, %s%, add.edges, add.vertices, delete.edges, delete.vertices,
get.edge.attribute, get.edges, get.vertex.attribute, is.bipartite,
is.directed, list.edge.attributes, list.vertex.attributes,
set.edge.attribute, set.vertex.attribute
Code
library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.1.3
v ggplot2 3.4.0 v purrr 0.3.5
v tibble 3.1.8 v dplyr 1.0.10
v tidyr 1.2.1 v stringr 1.5.0
v readr 2.1.3 v forcats 0.5.2
Warning: package 'ggplot2' was built under R version 4.1.3
Warning: package 'tibble' was built under R version 4.1.3
Warning: package 'tidyr' was built under R version 4.1.3
Warning: package 'readr' was built under R version 4.1.3
Warning: package 'purrr' was built under R version 4.1.3
Warning: package 'dplyr' was built under R version 4.1.3
Warning: package 'stringr' was built under R version 4.1.3
Warning: package 'forcats' was built under R version 4.1.3
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x dplyr::as_data_frame() masks tibble::as_data_frame(), igraph::as_data_frame()
x purrr::compose() masks igraph::compose()
x tidyr::crossing() masks igraph::crossing()
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
x purrr::simplify() masks igraph::simplify()
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.
Code
head(got_marriages)
# A tibble: 6 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
degree indegree outdegree
Min. :0.000 Min. :0.000 Min. :0.000
1st Qu.:3.000 1st Qu.:1.000 1st Qu.:1.000
Median :4.000 Median :2.000 Median :2.000
Mean :3.883 Mean :1.942 Mean :1.942
3rd Qu.:5.000 3rd Qu.:3.000 3rd Qu.:3.000
Max. :9.000 Max. :6.000 Max. :6.000
Source Code
---title: "Week 3 Challenge Instructions"author: "Mani Kanta Gogula"description: "Degree and Density of a Network"date: "03/06/2023"format: html: toc: true code-fold: true code-copy: true code-tools: true# editor: visualcategories: - challenge_3 - instructions # - railroads # - faostat # - wildbirds---```{r}#| label: setup#| include: false#| ``````{r}library(igraph)library(network)library(tidyverse)``````{r}library(readr)got_marriages <-read_csv("_data/got/got_marriages.csv")head(got_marriages)got_marriages.ig <-graph_from_data_frame(got_marriages, directed =TRUE)``````{r}# number of edgesecount(got_marriages.ig)# number of verticesvcount(got_marriages.ig)# vertex and edge attributesvertex_attr_names(got_marriages.ig)edge_attr_names(got_marriages.ig)# network featuresis_directed(got_marriages.ig)is_bipartite(got_marriages.ig)is_weighted(got_marriages.ig)# dyad censusigraph::dyad.census(got_marriages.ig)# triad censustriad_census(got_marriages.ig)```This network has 255 edges and 20 vertices. The vertex attribute is 'name' and edge attributes are "Type", "Notes", "Generation". ```{r}# number of componentsigraph::components(got_marriages.ig)$no# size of componentsigraph::components(got_marriages.ig)$csize ```Compute the density of the network. Is this a global or local measure? Does it have a relationship with average degree? ```{r}# network densitygraph.density(got_marriages.ig)# density without loopsgraph.density(got_marriages.ig, loops=TRUE)``````{r}# average network degreeigraph::degree(got_marriages.ig)``````{r}nodes_ig<-data.frame(name=V(got_marriages.ig)$name, degree=igraph::degree(got_marriages.ig,loops=FALSE))nodes_ig<-nodes_ig %>%mutate(indegree=igraph::degree(got_marriages.ig, mode="in", loops=FALSE),outdegree=igraph::degree(got_marriages.ig, mode="out", loops=FALSE))head(nodes_ig)``````{r}erdos_renyi.ig <-sample_gnm(103, 200, directed =TRUE, loops =FALSE)# density of random networkgraph.density(erdos_renyi.ig)# dyad census of random networkigraph::dyad.census(erdos_renyi.ig)# triad census of random networkigraph::triad.census(erdos_renyi.ig)``````{r}nodes_rand<-data.frame(degree=igraph::degree(erdos_renyi.ig))nodes_rand<-nodes_rand %>%mutate(indegree=igraph::degree(erdos_renyi.ig, mode="in", loops=FALSE),outdegree=igraph::degree(erdos_renyi.ig, mode="out", loops=FALSE))head(nodes_rand)``````{r}summary(nodes_rand)```