Discussion:
[igraph] Converting Igraph object to adjacency matrix of class "matrix"
Filippo Santi
2018-02-27 08:30:23 UTC
Permalink
Hello Everyone,

I am trying to obtain an adjacency matrix from a graph object, as I have to
perform some manipulation before reconverting it to a graph to obtain the
statistics I need.

However, I am stuck with a dgcMatrix, as I am not able to convert it to a
standard "matrix" class adjacency matrix.

Below the code I am using:

# Creating network from edgelist
*undesa.overind.1990 <- graph.data.frame(edge, vertices = vert, directed =
T)*
*undesa.overind.1990 <- set_edge_attr(undesa.overind.1990, 'weight', value
= get.edge.attribute(undesa.overind.1990, 'stock_od'))*

# Extracting adjacency matrix
*adjac <- as_adjacency_matrix(undesa.overind.1990, edges = T, names = T,
attr = 'weight')*
*class(adjac) #dgMatrix*

I tried to convert with

*as(adjac, "matrix")*

However, what I get is a matrix with "the non-zero elements in the columns
sorted into increasing row order", as explained by the dgCMatrix help file,
rather than the adjacency matrix.

How could I solve this issue, to retrieve the adjacency matrix from my
graph? I could not find any useful help online.

Best,

Filippo
--
Filippo Santi
PhD Candidate in Development Economics
Gábor Csárdi
2018-02-27 09:33:22 UTC
Permalink
Hi, set sparse = FALSE in as_adjacency_matrix.

Gabor
Post by Filippo Santi
Hello Everyone,
I am trying to obtain an adjacency matrix from a graph object, as I have to
perform some manipulation before reconverting it to a graph to obtain the
statistics I need.
However, I am stuck with a dgcMatrix, as I am not able to convert it to a
standard "matrix" class adjacency matrix.
# Creating network from edgelist
undesa.overind.1990 <- graph.data.frame(edge, vertices = vert, directed = T)
undesa.overind.1990 <- set_edge_attr(undesa.overind.1990, 'weight', value =
get.edge.attribute(undesa.overind.1990, 'stock_od'))
# Extracting adjacency matrix
adjac <- as_adjacency_matrix(undesa.overind.1990, edges = T, names = T, attr
= 'weight')
class(adjac) #dgMatrix
I tried to convert with
as(adjac, "matrix")
However, what I get is a matrix with "the non-zero elements in the columns
sorted into increasing row order", as explained by the dgCMatrix help file,
rather than the adjacency matrix.
How could I solve this issue, to retrieve the adjacency matrix from my
graph? I could not find any useful help online.
Best,
Filippo
--
Filippo Santi
PhD Candidate in Development Economics
_______________________________________________
igraph-help mailing list
https://lists.nongnu.org/mailman/listinfo/igraph-help
Filippo Santi
2018-02-27 09:50:37 UTC
Permalink
Thank you Gabor,

I set it to false and now it works.

Best,

Filippo
Post by Gábor Csárdi
Hi, set sparse = FALSE in as_adjacency_matrix.
Gabor
Post by Filippo Santi
Hello Everyone,
I am trying to obtain an adjacency matrix from a graph object, as I have
to
Post by Filippo Santi
perform some manipulation before reconverting it to a graph to obtain the
statistics I need.
However, I am stuck with a dgcMatrix, as I am not able to convert it to a
standard "matrix" class adjacency matrix.
# Creating network from edgelist
undesa.overind.1990 <- graph.data.frame(edge, vertices = vert, directed
= T)
Post by Filippo Santi
undesa.overind.1990 <- set_edge_attr(undesa.overind.1990, 'weight',
value =
Post by Filippo Santi
get.edge.attribute(undesa.overind.1990, 'stock_od'))
# Extracting adjacency matrix
adjac <- as_adjacency_matrix(undesa.overind.1990, edges = T, names = T,
attr
Post by Filippo Santi
= 'weight')
class(adjac) #dgMatrix
I tried to convert with
as(adjac, "matrix")
However, what I get is a matrix with "the non-zero elements in the
columns
Post by Filippo Santi
sorted into increasing row order", as explained by the dgCMatrix help
file,
Post by Filippo Santi
rather than the adjacency matrix.
How could I solve this issue, to retrieve the adjacency matrix from my
graph? I could not find any useful help online.
Best,
Filippo
--
Filippo Santi
PhD Candidate in Development Economics
_______________________________________________
igraph-help mailing list
https://lists.nongnu.org/mailman/listinfo/igraph-help
_______________________________________________
igraph-help mailing list
https://lists.nongnu.org/mailman/listinfo/igraph-help
--
Filippo Santi
PhD Candidate in Development Economics
Loading...