Efficient way to get graph communities

Viewed 87

I have the following type of data where I know there will be lots of unconnected communities (good separation). I want to efficiently (fast and preferably low/no dependencies) separate the data into their communities. I know I can use igraph to do this task but was hoping there's a fast base R way to extract these communities. I show how I separate the communities using I graph below.

Is there a fast, base R way to extract communities? Sharing non-base R approaches is fine too so that this question is more useful to future searchers.

dat <-  data.frame(
    x = c('A', 'A', 'B', 'C',    'D', 'F',   'E',    'W', 'X', 'R', 'W'),
    y = c('A', 'B', 'C', 'C',    'F', 'F',   'E',    'Y', 'P', 'P', 'P')
)

mat <- xtabs(~ x + y, data = dat)

library(igraph)
g <- graph.data.frame(dat)
plot(g)

clust <- cluster_walktrap(g)

data.frame(
    val = clust$names,
    group = clust$membership
)

Desired Output

##    val group
## 1    A     2
## 2    B     2
## 3    C     2
## 4    D     3
## 5    F     3
## 6    E     4
## 7    W     1
## 8    X     1
## 9    R     1
## 10   Y     1
## 11   P     1

enter image description here

0 Answers
Related