简体   繁体   中英

R: Looping social network analysis

I'm trying to calculate some network measures in R using the igraph apckage. At first I read my data, than transform it, and eventually bring it to an adjacency matrix form. Then I calculate the degree values for this dataset.

I would like to put my code in a loop, so I wouldn't need to manually change the conditions - re-read the data - and run the code again. But I had no luck. The "r" condition may be "1" or "2", and the "g" condition is a number from "1" to "21".

My code looks like this so far:

p1 <- read.csv("matrix.csv")
p <- p1[p1$r=="1" & p1$g == "1",]
rownames(p) <- paste("id", p$id, sep=".")
p <- t(p[,-(1:3)])
p <- data.frame(p)
rr <- apply(p, 2, as.character)

m<-matrix(nrow=ncol(rr),ncol=ncol(rr))
for(col in 1:ncol(rr)){
  matches<-rr[,col]==rr
  match.counts<-colSums(matches)
  match.counts[col]<-0
  m[,col]<-match.counts
}

n<-graph.adjacency(m)

d<-degree(n, v=V(n))

The first few rows of "p1":

1> p1
    id     g   r X1        X2         X3       X4
1    1     1   1 1324;1256 1324;1256  752;1268 1892;1236
2    2     1   2   324;988   324;988   324;988   324;988
3    3     1   1 1312;1652 1312;1652  1828;608   712;656
4    4     1   2   324;988   324;988   324;988   324;988 ...

I know my code is quite ugly... I have no prior programming experience, but I am eager to learn, so I welcome any kind of suggestion or advice.

Thanks in advance!

Here I'm assuming that the first 3 columns are identifiers and any following columns describe the graph in the format V1;V2 where V1 and V2 are the vertex ids.

Just taking a small part of your data frame, here is what I've come up with. I don't think you need to create an adjacency matrix because you can make an edge list.

require(igraph)
p1 <- read.csv(textConnection(
"id,g,r,X1,X2,X3,X4                                                                                                                                                                  
1,1,1,1324;1256,1324;1256,752;1268,1892;1236                                                                                                                                                                       
2,1,2,324;988,324;988,324;988,324;988                                                                                                                                                                              
3,1,1,1312;1652,1312;1652,1828;608,712;656                                                                                                                                                                         
4,1,2,324;988,324;988,324;988,324;988"))

To do this for one value of r and g:

p <- p1[p1$r=="1" & p1$g == "1",] ## limit to only rows where r and g are 1

myEdges <- p[,-(1:3)] ## assuming edges are defined in all columns after the first 3                                                                                                                                                  
dat <- apply(myEdges, 1, function(strings) unlist(strsplit(strings, ';', fixed=TRUE)))
myGraph <- graph.data.frame(dat, directed=FALSE) # can change to directed by setting directed = TRUE
plot(myGraph) # see what the graph looks like, don't try if graph is large!

degree(myGraph)
# 1324 1256  752 1268 1892 1236 1312 1652 1828  608  712  656
#   2    2    1    1    1    1    2    2    1    1    1    1

To answer your comment about automating the process for different combinations of r and g, you can use an approach like this with nested for loops (not very efficient but may work for your problem depending on the size)

rVals <- 1:2
gVals <- 1:21
myGraphList <- rep( list(vector(mode = "list", length = length(gVals) )), length(rVals))
for(r in rVals) {
  for(g in gVals) {
    p <- p1[p1$r == r & p1$g == g,] ## limit to only certain values of r and g                                                                                                                                 
    myEdges <- p[,-(1:3)] ## assuming edges are defined in all columns after the first 3                                                                                                                       
    if(nrow(myEdges) > 0) { ## Only need to create a graph if there are edges present                                                                                                                          
      dat <- apply(myEdges, 1, function(strings) unlist(strsplit(strings, ';', fixed=TRUE)))
      myGraphList[[r]][[g]] <- graph.data.frame(dat, directed=FALSE)
    }
  }
}

## Only 2 elements with an igraph object in this small example:                                                                                                                                                
degree(myGraphList[[1]][[1]]) # when r == 1, g == 1                                                                                                                                                            
# 1324 1256  752 1268 1892 1236 1312 1652 1828  608  712  656                                                                                                                                                  
#   2    2    1    1    1    1    2    2    1    1    1    1                                                                                                                                                   
degree(myGraphList[[2]][[1]]) # when r == 2, g == 1                                                                                                                                                            
# 324 988                                                                                                                                                                                                      
#  8   8  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM