O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Extracting an adjacency matrix with weights

The graph.data.frame function from the igraph package treats the first two columns of the data frame supplied as making up the edge list and the rest of the columns as edge attributes. By default, the get.adjacency function does not return any edge attributes and instead returns a simple 0-1 sparse matrix of connections.

However, you can pass the attr argument to tell the function which of the remaining attributes you want as the elements of the sparse matrix (and hence the edge weight). In our situation, this attribute will be x, representing the number of common group memberships between two users. In the following, we have specified type="lower" to get the lower triangular matrix. Other options ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required