Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Various functions for converting social network data into different formats in R
csvtomatrix <- function (Y) {
# Converts CSV into data matrix
Y.m<-data.matrix(Y)
Y.m<-Y.m[,-1]
dimnames(Y.m) <- list (Y[,1],Y[,1])
diag(Y.m)<-NA
# Returns data matrix
Y.m
}
csvtoedge <- function (Y) {
# Converts CSV into data matrix
Y.m<-csvtomatrix(Y)
# Enables igraph, outputs edge list
library(igraph)
Y.g <- graph.adjacency(Y.m)
Y.el<-get.edgelist(Y.g)
detach(package:igraph)
Y.el
}
csvtodot <- function (Y) {
# Converts CSV into edge list
Y.el<-csvtoedge(Y)
# Outputs list in DOT format
Y.out<-"digraph G { \n"
for (i in 1:nrow(Y.el)) {
Y.out<-paste(Y.out,"\t",Y.el[i,1],"->",Y.el[i,2],";\n", sep="")
}
Y.out<-paste(Y.out,"}")
# Makes filename out of variable name
Y.filename<-paste(deparse(substitute(Y)),"edgelist.dot",sep="")
# Writes to File
write(Y.out,Y.filename)
}
netcorr <- function (X,Y) {
library(sna)
Y.output<-NULL
# Correlation Commands
Y.output$incor<-cor(degree(X, cmode="indegree"), degree(Y, cmode="indegree"))
dycor<-qaptest(list(X,Y),gcor,g1=1,g2=2)
Y.output$dycor<-dycor$testval
Y.output$stcor<-gscor(X,Y, diag=FALSE)
detach(package:sna)
# Reports Results
Y.output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.