本文介绍了识别R中的集团的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个像这样的数据框:
I have a dataframe like this:
1 2
2 3
4 5
....
现在,我使用以下代码使用库igraph在R中绘制此图:
Now, I plot this graph in R using the library igraph using the following code:
wt=read.table("NP7.txt")
wt1=matrix(nrow=nrow(wt), ncol=2)
wt1=data.frame(wt1)
wt1[,1:2]=wt[,1:2]
write.table(wt1,"test.txt")
library(igraph)
wt=read.table("test.txt")
wg7 <- graph.edgelist(cbind(as.character(wt$X1), as.character(wt$X2)),
directed=F)
sum(clusters(wg7)$csize>2)
plot(wg7)
a <- largest.clique(wg7)
现在,在运行此代码时,我得到了图形的图和形成最大集团的值.但是,如果我想要那个实际最大的集团的情节,我该怎么做?谢谢!
Now, on running this code I get the plot of the graph and the values that form the largest clique. But, if I want the plot of that actual largest clique, how do I do that?Thanks!
推荐答案
下面是一个示例:
library(igraph)
# for reproducibility of graphs plots (plot.igraph uses random numbers)
set.seed(123)
# create an example graph
D <- read.table(header=T,text=
'from to
A B
A C
C D
C F
C E
D E
D F
E F')
g1 <- graph.data.frame(D,directed=F)
# plot the original graph
plot(g1)
# find all the largest cliques (returns a list of vector of vertiex ids)
a <- largest.cliques(g1)
# let's just take the first of the largest cliques
# (in this case there's just one clique)
clique1 <- a[[1]]
# subset the original graph by passing the clique vertices
g2 <- induced.subgraph(graph=g1,vids=clique1)
# plot the clique
plot(g2)
图1(原始图):
图2(斜线):
正如@GaborCsardi正确指出的那样,由于集团是完整的图,因此不必对图进行子集化.这可能比induced.subgraph
更有效:
As correctly pointed out by @GaborCsardi, it is not necessary to subset the graph since a clique is a complete graph. This is probably more efficient than induced.subgraph
:
g2 <- graph.full(length(clique1))
V(g2)$name <- V(g1)$name[clique1]
这篇关于识别R中的集团的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!