I'm converting a matrix object (where each node represents a country) into a dataframe object such that the first two columns are the two nodes connecting an edge and the third column is the edge value (or attribute). The dimension of my matrix is 161 by 161, so the resulting dataframe should contain 161*161 = 25,961 rows. However, the following code was only able to produce 19,628 rows, I am wondering what's going on here? It will be really appreciated if someone could shed some lights on this.
# load data
sim <- readRDS(url("https://www.dropbox.com/s/veyjr8mdi1u6g6h/sim.rds?dl=1"))
# load igraph package and use the graph.adjacency() function
# first try with a minimal example
myAdjacencyMatrix <- matrix(runif(100),nc=10,nr=10)
g <- graph.adjacency(myAdjacencyMatrix,weighted=TRUE)
df <- get.data.frame(g)
dim(df)
[1] 100 3
# now repeat the same procedure on sim data
g1 <- graph.adjacency(sim,weighted=TRUE)
df1 <- get.data.frame(g1)
dim(df1)
[1] 19628 3
CodePudding user response:
When you check the values of sim
, you will see that there are 6293
zeros, which are skipped when transferred to the graph representation.
> ecount(g1)
[1] 19628
> length(sim)
[1] 25921
> sum(sim==0)
[1] 6293