Lucia Lucia - 2 months ago 16
R Question

generating adjacency table with R

I generated an adjacency table

mytable
with cosine similarity,
m1
is a DTM

cosineSim <- function(x){
as.dist(x%*%t(x)/(sqrt(rowSums(x^2) %*% t(rowSums(x^2)))))
}

cs <- cosineSim(m1)


mytable


"";"1";"2";"3";"4";"5";"6";"7";"8"
"1";0;0;0;0;0;0;0;0
"2";0;0;0;0;0;0;0;0
"3";0;0;0;0.259;0;0;0;0
"4";0;0;0;0;0;0;0;0.324
"5";0;0;0;0;0;0;0;0
"6";0;0;0;0;0;0;0;0
"7";0;0;0;0;0;0;0;0
"8";0;0;0;0;0;0;0;0


When I open it with Gephi, I find that the nodes include all the numbers in the table

Id label
" "
1" 1"
2" 2"
3" 3"
4" 4"
5" 5"
6" 6"
7" 7"
8 8
0 0
0.259 0.259
0.324 0.324
8" 8"


I expected the nodes only include 1-8 as ids, not "", "0 and other numbers. Is there something wrong with my adjacency table?

Answer

Remove the double quotes and try to reimport. Since you are using R I would propose to automate your pipeline by using igraph and in your case graph_from_adjacency_matrix, cf here. Then you will need to export the graph in GraphML which Gephi can easily read

Here is some example code for the sake of completeness:

library(igraph)
t <- ';1;2;3;4;5;6;7;8
1;0;0;0;0;0;0;0;0
2;0;0;0;0;0;0;0;0
3;0;0;0;0.259;0;0;0;0
4;0;0;0;0;0;0;0;0.324
5;0;0;0;0;0;0;0;0
6;0;0;0;0;0;0;0;0
7;0;0;0;0;0;0;0;0
8;0;0;0;0;0;0;0;0'

f <- read.csv(textConnection(t), sep = ";", header = T, row.names = 1)
m <- as.matrix(f, rownames.force = T)
colnames(m) <- seq(1:dim(f)[1])
rownames(m) <- seq(1:dim(f)[1])
graph <- graph_from_adjacency_matrix(m, mode=c("directed"), weighted = T)
write.graph(graph, "mygraph.graphml", format=c("graphml") )
Comments