Reputation: 2203
I have a network. In which the nodes are connected like this :
A-B
B-C
A-C
B-D
E-F
F-G
H-G
H-E
I want them to be clustered like ABCD and EFGH. Is there a way to do it in R??
Upvotes: 0
Views: 985
Reputation: 1383
Thought I'd bang together a direct solution to provide a useful comparison (and practice my coding - all pointers welcome...)
## initial link data frame (with start and end points)
link=data.frame(st=c("A","B","A","B","E","F","H","H"),
end=c("B","C","C","D","F","G","G","E"),
stringsAsFactors=FALSE)
## form blank list where clusters will build up (works up
## to n=26!)
n=nrow(link)
L=c(); for (j in seq_len(n)) {L=c(L,list(NULL))}
names(L)=LETTERS[seq_len(n)]
## for each link in turn, pull together a collection of
## everything in the same cluster as either end, and update
## all relevant entries in L
for (j in seq_len(n)) {
clus=sort(unique(c(link$st[j],link$end[j],
L[[link$st[j]]],L[[link$end[j]]])))
for (k in clus) {L[[k]]=clus}
}
print(L)
The output is, as expected:
$A
[1] "A" "B" "C" "D"
$B
[1] "A" "B" "C" "D"
$C
[1] "A" "B" "C" "D"
$D
[1] "A" "B" "C" "D"
$E
[1] "E" "F" "G" "H"
$F
[1] "E" "F" "G" "H"
$G
[1] "E" "F" "G" "H"
$H
[1] "E" "F" "G" "H"
Upvotes: 1
Reputation: 20969
That is called "Connected Components" of a graph and is present in the igraph
package.
Here is the doc:
http://igraph.sourceforge.net/doc/R/clusters.html
Upvotes: 3