I want to create an adjacency matrix to use in social network analysis (likely with graph_from_adjacency_matrix in igraph) from a csv that is structured like this (but much larger):
name vote1 vote2 vote3
Joe 1 0 1
Jane 0 0 1
Jill 1 0 1
For the network analysis, the node will be the name variable, and the nodes will be connected by how frequently they vote together (1 or 0). Something like:
Joe Jane Jill
Joe 0 2 3
Jane 2 0 2
Jill 3 2 0
As simple as this seems, I haven't been able to successfully convert this dataframe into an adjacency matrix that can be used to create an igraph graph object. as.matrix and data.matrix do convert it to a matrix, but not an adjacency matrix, and not one that preserves the characters in the "name" variable. My matrix algebra is not strong, so I know I'm likely missing something obvious, but I don't know enough to know what it is. I'm open to other solutions that get me to my end goal of network analysis.
I think you want some version of the cross product.
The off diagonal shows the counts of instances where the individuals voted at the same time and the diagonal gives the counts of how many times the individuals voted with themselves (ie, their total vote count).
Since you don't want the total vote counts of each individual, you can replace the diagonal with 0s.
Adding two additional votes and two additional voters in df1 below. There is a voter named Sal who only votes once in vote 2 and is the only voter.
Running through the above process with this larger matrix, we get
Which shows 0s in all of Sal's slots and 3s in Bob-Jill Jill-Bob slots as they both voted in the same 3 votes.
data