Skip to content

Instantly share code, notes, and snippets.

@rrodrigueznt
Last active May 26, 2017 12:01
Show Gist options
  • Save rrodrigueznt/5586456 to your computer and use it in GitHub Desktop.
Save rrodrigueznt/5586456 to your computer and use it in GitHub Desktop.
Generates adjacency matrices and graphs
############################################
### Reshaping data from IDIS Excel files ###
############################################
# This Gist arranges Excel format in a suitable way for creating matrices, adjacency matrices, etc
# Based on a Gist recently created by Aurora Baluja
# The example uses an excerpt from a real database belonging to the Health Research Institute of Santiago de Compostela
# https://gist.github.com/aurora-mareviv/5572111#file-gistfile1-r
# First, let's access the datasheet data stored in a remote repository:
library(gdata)
IDIS.publications <- "http://atriumkm.idisantiago.es/bin/download/IDIS/DataRepositoryFreeAccess/IDIS.publications.fa.xlsx"
IDISpublications <- read.xls(IDIS.publications, sheet='publications')
# Trim extra-blanks from all cells and subset data concerning research areas' contribution to scientific papers
IDISpublicationsTrimmed <- trim(IDISpublications[,c(1,14:16)])
# Let's transform the data to long format:
library(reshape)
IDISpublicationsTrimmedMelted <- melt(IDISpublicationsTrimmed, id="pubId")
# Now we remove the "variable" column:
IDISpublicationsTrimmedMelted$variable <- NULL
# We remove nulls:
IDISpublicationsTrimmedMelted <- IDISpublicationsTrimmedMelted[!(IDISpublicationsTrimmedMelted$value == ""), ]
# And, finally, cast data to the desired format...
PubAreaMat <- cast(IDISpublicationsTrimmedMelted, pubId ~ value, length)
# ... and generate a matrix depicting interactions; a seventh column and row shows sums
PubAreaMat <- as.matrix(PubAreaMat)
adj_PubAreaMat = t(PubAreaMat) %*% (PubAreaMat)
addmargins(adj_PubAreaMat)
# ... and a graph, simple, extremely simple, using just one color, not modifying positions or anything else!
library(igraph)
G <- graph.incidence(as.matrix(PubAreaMat),weighted=TRUE,directed=FALSE)
V(G)[1:5]$color <- "#D6E1E7"
V(G)[69]$color <- "red"
V(G)[c(34,78:80)]$color <- "green"
plot(G)
# The coincidence is 100% with a handmade matrix.
@rrodrigueznt
Copy link
Author

Call me, maybe!

library(RCurl)
eval(expr=parse(text=getURL("copy/paste here the Gist release you want to use!",ssl.verifypeer=FALSE)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment