The following objects are masked from 'package:stats':
decompose, spectrum
The following object is masked from 'package:base':
union
Code
library(network)
'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)
-- Attaching core tidyverse packages ------------------------ tidyverse 2.0.0 --
v dplyr 1.1.2 v readr 2.1.4
v forcats 1.0.0 v stringr 1.5.0
v ggplot2 3.4.2 v tibble 3.2.1
v lubridate 1.9.2 v tidyr 1.3.0
v purrr 1.0.1
-- Conflicts ------------------------------------------ tidyverse_conflicts() --
x lubridate::%--%() masks igraph::%--%()
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()
i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Rows: 33 Columns: 5
-- Column specification --------------------------------------------------------
Delimiter: ","
chr (3): team, alliance, joins
dbl (2): id, gh
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_teams)
# A tibble: 6 x 5
team id alliance joins gh
<chr> <dbl> <chr> <chr> <dbl>
1 Drogo 0 Opportunists follows Drogo 0
2 Littlefinger 1 Opportunists follows Littlefinger 0
3 Martell 2 Opportunists joins the Martells 0
4 Red God 3 Opportunists worships the Red God 0
5 Renly 4 Opportunists follows Renly 0
6 Second Sons 5 Opportunists joins the Second Sons 0
name degree indegree outdegree
Drogo Drogo 1 0 1
Littlefinger Littlefinger 1 0 1
Martell Martell 1 0 1
Red God Red God 1 0 1
Renly Renly 1 0 1
Second Sons Second Sons 1 0 1
Code
erdos_renyi.igraph <-sample_gnm(103, 200, directed =TRUE, loops =FALSE)# density of random networkgraph.density(erdos_renyi.igraph)
[1] 0.01903674
Code
# dyad census of random networkigraph::dyad.census(erdos_renyi.igraph)
$mut
[1] 3
$asym
[1] 194
$null
[1] 5056
Code
# triad census of random networkigraph::triad.census(erdos_renyi.igraph)
Error in mutate(., indegree = igraph::degree(erdos_renyi.igraph, mode = "in", : object 'rand_nodes' not found
Code
head(random_nodes)
degree
1 5
2 2
3 4
4 7
5 7
6 4
Code
summary(random_nodes)
degree
Min. : 0.000
1st Qu.: 3.000
Median : 4.000
Mean : 3.883
3rd Qu.: 5.000
Max. :10.000
Source Code
---title: "Week3_Challenge_Rahul Gundeti"author: "Rahul Gundeti"description: "Degree and Density of a Network"date: "03/10/2023"format: html: toc: true code-fold: true code-copy: true code-tools: true# editor: visualcategories: - challenge_3 - instructions---```{r}#| label: setup#| include: false``````{r, warning=FALSE}#loading librarieslibrary(igraph)library(network)library(tidyverse)library(readr)``````{r}#Loading datasetgot_teams <-read_csv("_data/got/got_teams.csv")head(got_teams)got_teams.igraph <-graph_from_data_frame(got_teams, directed =TRUE)``````{r}# number of edgesecount(got_teams.igraph)# number of verticesvcount(got_teams.igraph)# vertex and edge attributesvertex_attr_names(got_teams.igraph)edge_attr_names(got_teams.igraph)# network featuresis_directed(got_teams.igraph)is_bipartite(got_teams.igraph)is_weighted(got_teams.igraph)# dyad censusigraph::dyad.census(got_teams.igraph)# triad censustriad_census(got_teams.igraph)```The vertex attribute is 'name' and edge attributes are "alliance", "joins", "gh" and this network has 255 edges and 20 vertices.```{r}# number of componentsigraph::components(got_teams.igraph)$no# size of componentsigraph::components(got_teams.igraph)$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_teams.igraph)# density without loopsgraph.density(got_teams.igraph, loops=TRUE)``````{r}# average network degreeigraph::degree(got_teams.igraph)``````{r}nodes_igraph<-data.frame(name=V(got_teams.igraph)$name, degree=igraph::degree(got_teams.igraph,loops=FALSE))nodes_igraph<-nodes_igraph %>%mutate(indegree=igraph::degree(got_teams.igraph, mode="in", loops=FALSE),outdegree=igraph::degree(got_teams.igraph, mode="out", loops=FALSE))head(nodes_igraph)``````{r}erdos_renyi.igraph <-sample_gnm(103, 200, directed =TRUE, loops =FALSE)# density of random networkgraph.density(erdos_renyi.igraph)# dyad census of random networkigraph::dyad.census(erdos_renyi.igraph)# triad census of random networkigraph::triad.census(erdos_renyi.igraph)``````{r}random_nodes<-data.frame(degree=igraph::degree(erdos_renyi.igraph))random_nodes<-rand_nodes %>%mutate(indegree=igraph::degree(erdos_renyi.igraph, mode="in", loops=FALSE),outdegree=igraph::degree(erdos_renyi.igraph, mode="out", loops=FALSE))head(random_nodes)``````{r}summary(random_nodes)```