我正在尝试根据我的数据创建有价值的二分投影,如下所示:
Name rid
Emile 17560
Lou 11800
Luther 11800
Tot 11800
Phineas 11800
Phineas 13580
Calvin 13580
Calvin 11800
Les 11800
Jeff 11800
Sonny 13580
Leon 13580
Red 13580
我导入了上述数据并将其命名为data1
然后我执行以下操作:
##create graph object from data1
graph1 <- graph.data.frame(data1)
#check if it is bipartite
is.bipartite(graph1)
FALSE
##convert to bipartite graph
V(graph1)$type <- V(graph1)$name %in% data1[,1]
##check again if it is bipartite
is.bipartite(graph1)
TRUE
##create biparite projection
##the multiplicity argument is suppose to count the number of
##edges
proj<-bipartite.projection(graph1, V(graph1)$type,multiplicity = TRUE)
##get adjacency matrix from second projection
get.adjacency(proj$proj2)
我得到以下输出
11 x 11 sparse Matrix of class "dgCMatrix"
[[ suppressing 11 column names ‘Emile ’, ‘Lou ’, ‘Luther ’ ... ]]
Emile . . . . . . . . . . .
Lou . . 1 1 1 . 1 1 1 . .
Luther . 1 . 1 1 . 1 1 1 . .
Tot . 1 1 . 1 . 1 1 1 . .
Phineas . 1 1 1 . . 1 1 1 . .
Phineas . . . . . . 1 . . 1 1
Calvin . 1 1 1 1 1 . 1 1 1 1
Les . 1 1 1 1 . 1 . 1 . .
Jeff . 1 1 1 1 . 1 1 . . .
Sonny . . . . . 1 1 . . . 1
Leon . . . . . 1 1 . . 1 .
和
3 x 3 sparse Matrix of class "dgCMatrix"
17560 11800 13580
17560 . . .
11800 . . 1
13580 . 1 .
而不是 Phineas 和 Calvin 通过*** rid *** s 13580和11800有两条边,Phineas出现两次。并且“摆脱”投影的图表在13580和11800之间仅显示1个边缘。“名称”的图表显示Phineas两次。
我感谢任何修改此代码以获得有价值的投影和邻接矩阵的建议。谢谢!
编辑#1 :由于某种格式问题导致两个“Phinease-s”被识别为单独的名称,Phineas出现了两次。但是,解决这个问题仍然无法解决主要问题。输出仍然只在Phineas和Calvin之间以及13580和11800之间提供了1个边缘。
编辑#2:会话信息结果
R version 3.1.0 (2014-04-10)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] igraph_1.0.1
loaded via a namespace (and not attached):
[1] grid_3.1.0 lattice_0.20-29 magrittr_1.5 Matrix_1.1-3 tools_3.1.0
\
编辑#3
Desired output
3 x 3 sparse Matrix of class "dgCMatrix"
17560 11800 13580
17560 . . .
11800 . . 2
13580 . 2 .
11 x 11 sparse Matrix of class "dgCMatrix"
[[ suppressing 11 column names ‘Emile ’, ‘Lou ’, ‘Luther ’ ... ]]
Emile . . . . . . . . . . .
Lou . . 1 1 1 . 1 1 1 . .
Luther . 1 . 1 1 . 1 1 1 . .
Tot . 1 1 . 1 . 1 1 1 . .
Phineas . 1 1 1 . 2 1 1 1 1
Calvin . 1 1 1 2 1 . 1 1 1 1
Les . 1 1 1 1 . 1 . 1 . .
Jeff . 1 1 1 1 . 1 1 . . .
Sonny . . . . . 1 1 . . . 1
Leon . . . . . 1 1 . . 1 .
答案 0 :(得分:0)
我不知道你做了什么。可能还需要更多信息:sessionInfo()来检查您使用的系统以及软件包的版本。
这是我在PC上找到的(见下面sessionInfo()的结果
数据
data1 <- read.table(text="Name rid
Emile 17560
Lou 11800
Luther 11800
Tot 11800
Phineas 11800
Phineas 13580
Calvin 13580
Calvin 11800
Les 11800
Jeff 11800
Sonny 13580
Leon 13580
Red 13580", header=TRUE)
检查数据
head(data1)
dim(data1)
str(data1)
plot(data1)
调用库
library(igraph)
从data1
创建图形对象graph1 <- graph.data.frame(data1)
检查它是否为二分
is.bipartite(graph1) ### FALSE
转换为二分图
V(graph1)$type <- V(graph1)$name %in% data1[,1]
再次检查它是否为二分
is.bipartite(graph1) ### TRUE
创建双向投影 multiplicity参数假设计算边数
proj<-bipartite.projection(graph1, V(graph1)$type,multiplicity = TRUE)
从第一个投影中获取邻接矩阵
t1 <- get.adjacency(proj$proj1)
t1
加载必需的包:Matrix
11 x 11稀疏矩阵类“dgCMatrix”
[[压制11个专栏名称'Emile','Lou','Luther'......]
埃米尔。 。 。 。 。 。 。 。 。 。 娄。 。 1 1 1 1 1 1。 。 路德。 1。 1 1 1 1 1。 。总结。 1 1 1 1 1 1。 。
菲尼亚斯。 1 1 1。 1 1 1 1 1 1
凯文。 1 1 1 1。 1 1 1 1 1Les。 1 1 1 1 1。 1。 。
杰夫1 1 1 1 1 1。 。 。 桑尼。 。 。 。 1 1 。 。 1 1 莱昂。 。 。 。 1 1 。 1。 1红色。 。 。 。 1 1 。 1 1。
从第二次投影中获取邻接矩阵
t2 <- get.adjacency(proj$proj2)
t2
3 x 3稀疏矩阵类“dgCMatrix”
17560 11800 13580
17560。 。
11800。 。 1
13580。 1。
绘制矩阵
g1 <- graph.adjacency(t1)
plot(g1)
sessionInfo()的结果 R版本3.0.2(2013-09-25) 平台:x86_64-w64-mingw32 / x64(64位)
locale:
[1] LC_COLLATE=Italian_Italy.1252 LC_CTYPE=Italian_Italy.1252
[3] LC_MONETARY=Italian_Italy.1252 LC_NUMERIC=C
[5] LC_TIME=Italian_Italy.1252
attached base packages:[1] stats graphics grDevices utils datasets methods base
other attached packages: [1] Matrix_1.1-0 igraph_0.6.6
loaded via a namespace (and not attached): [1] grid_3.0.2 lattice_0.20-24