Created
May 9, 2019 15:20
-
-
Save semicolonsnet/b406815cbe86428891b88ea15fbc6825 to your computer and use it in GitHub Desktop.
Various functions for converting social network data into different formats in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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