setwd("...") #insert your work directory library ("igraph") library ("lsa") #for cosine normalization # Loading two-mode matrix with edge weights into matrix object M = as.matrix(read.csv(file.choose(), sep = ";", header = T, row.names = 1, check.names=FALSE)) class(M) #Transform matrix object into a weighted, directed, twomode igraph object (Cosine normalization works best with weighted matrices) two_mode_network <- graph.incidence(M, directed = TRUE, mode = "out", weighted = TRUE) # Delete isolates from the twomode network (optional) Isolated <- which(degree(two_mode_network)==0) two_mode_network_ni <- delete.vertices(two_mode_network, Isolated) # Transform twomode back to an incidence matrix M2 <- as_incidence_matrix(two_mode_network_ni, names = TRUE) # Make a heatmap of the incidence matrix (optional) heatmap (M2, Colv = NA, scale = "row", cexCol = 1, xlab = "Concept") #calculate cosine distances for actors and concepts actors_cosine <- cosine (t(M2)) concept_cosine <- cosine (M2) # create one-mode matrixes based on cosine normalization, set diagonals zero om_actors <- as.matrix(actors_cosine) diag(om_actors)<-0 om_concept <- as.matrix(concept_cosine) diag(om_concept)<-0 #Print to csv write.table (om_actors, file ="om_actors_cosine.csv", dec = ".", sep = ";", row.names = TRUE, col.names = NA, quote = FALSE) write.table (om_concept, file = "om_concepts_cosine.csv", dec = ".", sep = ";", row.names = TRUE, col.names = NA, quote = FALSE)