[英]R: Looping social network analysis
I'm trying to calculate some network measures in R using the igraph apckage. 我正在尝试使用igraph小贴士计算R中的一些网络度量。 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".
“ r”条件可以是“ 1”或“ 2”,而“ g”条件是从“ 1”到“ 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": “ 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. 在这里,我假设前三列是标识符,随后的任何列均以
V1;V2
格式描述图形,其中V1
和V2
是顶点ID。
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: 要对r和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) 为了回答有关针对r和g的不同组合自动执行过程的评论,您可以使用带有嵌套for循环的这种方法(效率不高,但根据大小可能会解决您的问题)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.