If it is not mandatory to use reshape
I'd suggest taking a look at igraph.
Here's one way to get the symmetrical matrix using the igraph
package. We first convert your data frame (the relevant 2 columns) into an igraph
object, and then get_adjacency
does the needful.
library(igraph)
g <- graph.data.frame(tab[,c(2,3)])
get.adjacency(g)
This gives you the adjacency matrix. You should definitely look into using igraph for the rest of your analysis.
16 x 16 sparse Matrix of class "dgCMatrix"
[[ suppressing 16 column names ‘ADRA1D’, ‘ADRA1B’, ‘ADRA1A’ ... ]]
ADRA1D . . . . . . . . . . 1 . . . . .
ADRA1B . . . . . . . . . . 1 . . . . .
ADRA1A . . . . . . . . . . 1 . . . . .
ADRB1 . . . . . . . . . . 1 1 . . . .
ADRB2 . . . . . . . . . . 1 1 . . . .
AGTR1 . . . . . . . . . . 1 . 1 . . .
ALOX5 . . . 1 1 . . . . . . . . . . .
ALPPL2 . . . 1 1 . . . . . . . . . . .
AMY2A . . . . . 1 . . . . . . . . . .
AR 1 1 1 . . . . . . . . . . 1 1 1
ADK . . . . . . . . . . . . . . . .
ASIC1 . . . . . . . . . . . . . . . .
ACHE . . . . . . . . . . . . . . . .
ADORA1 . . . . . . . . . . . . . . . .
ADRA2A . . . . . . . . . . . . . . . .
ADRA2B . . . . . . . . . . . . . . . .
An advantage of using igraph
is that many graph-based distance calculation methods are now available for you. Do look into shortest.paths