I'm working with network data and tidygraph in R. I'm trying to get yearly centrality measures without having to filter and merge, or iterate. Basically I want a node-year dataset that contains each nodes' eigenvector and degree centrality.
Tidygraph should do this, but I cannot find the proper way...
Below you find some code that reproduces my dataset and loads the packages
I'm working with R.Version 4.2.2 and tidygraph 1.2.3
Thanks!!
Livio
library(tidyverse)
library(manynet)
library(ggraph)
library(sna)
library(igraph)
library(tidygraph)
nodes <- tibble(
node_id = 1:6,
transaction_type = c("Purchase", "Deposit", "Withdrawal", "Transfer", "Payment", "Loan")
)
edges <- tibble(
from = c(1, 2, 3, 2, 4, 4, 5, 6, 5),
to = c(2, 4, 2, 3, 5, 6, 6, 4, 1),
year = c(2019, 2020, 2019, 2018, 2021, 2020, 2022, 2021, 2018),
amount = c(100, 500, 50, 200, 300, 150, 75, 1000, 250)
)
net <-graph_from_data_frame(d=edges, vertices=nodes, directed=T)
net %>% #one of my attempts...
as_tbl_graph()%>%
activate(nodes)%>%
group_by(.E()$year)%>%
mutate(eigen_centr=centrality_eigen(.E()))
as.tibble()
The structure of
net
is of a single graph representing all years, whereas you wish to measure the centrality of each node by year. As far as I know, the only two ways to do this areAlthough the second option sounds closer to what you are trying to do, it's not that simple. It would require adding a new column in the nodes data for each year. The first option seems easier to me, and doesn't require explicit loops:
The second version could be something like this, but requires specifically naming each year, which seems more difficult and error prone.