2

I have data on every interaction that could and did happen at a university club weekly social hour

id1     id2   timestalked   date  
 1       2       1         1/1/2010
 1       3       0         1/1/2010
...
100     2        4         1/8/2010
...

I want to first load this in as a directed graph for the entire time period for visualization. For the weighted matrix I did.

library(igraph);
el <- read.csv("el.csv", header = TRUE);
G <- graph.data.frame(el,directed=TRUE);
A <- as_adjacency_matrix(G,type="both",names=TRUE,sparse=FALSE,attr="timestalked");

I thought removing attr="timestalked" would turn the weights > 0 into 1 but that does not seem to work

library(igraph);
el <- read.csv("el.csv", header = TRUE);
G_unweight <- graph.data.frame(el,directed=TRUE);
A_unweight <- as_adjacency_matrix(G_unweight,type="both",names=TRUE,sparse=FALSE)
CJ12
  • 487
  • 2
  • 10
  • 28

1 Answers1

2

as_adjacency_matrix() doesn't provide any argument to control weights. Note that it just provides the number of edges between nodes from the graph.

To turn the weighted edgelist into an unweighted one, try this

A <- as_adjacency_matrix(G, type = "both", names = TRUE, sparse = FALSE)
A[A > 1] <- 1

Note that you can also use the graph_from_adjacency_matrix() function to create an unweighted igraph graph from the adjacency matrix by specifying weighted = NULL.

nghauran
  • 6,648
  • 2
  • 20
  • 29
  • Am I right in assuming that a weight of 0 does not draw a link between A and B? My weights are the number of interactions and I want the 0s to lead to no link – CJ12 Oct 27 '17 at 13:02
  • Yes, it's because after using `as_adjacency_matrix()`, 0s lead to no interaction (edge) between nodes. – nghauran Oct 27 '17 at 13:38