# Set your working directory setwd("C:/_YOUR_DIRECTORY_") library ("igraph") library ("lsa") #for cosine normalization # Loading two-mode matrix with edge weights into matrix object M <- as.matrix(read.csv2(file.choose(), 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_from_biadjacency_matrix(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_biadjacency_matrix(two_mode_network_ni, attr = "weight") # 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.csv2(om_actors, file ="om_actors_cosine.csv") write.csv2(om_concept, file = "om_concepts_cosine.csv")