Skip to content

How to aggregate combinations

3 messages · Rui Barradas, Oritteropus

#
Hi all, 
Given a table like the one below, I want to get a number of vectors equal to
the groups of connected ID (ID are considered connected if they are in the
same row). Each vector should contains all the connected ID . 

e.g. In this case: vect1  (1,2,3)         vect2  (5,6)     vect3 (7,8,9)

ID ID2 
1    2 
1    3 
2    3 
6    5 
7    8
8    9

Does someone know how to do it automatically for tables with thousands of
rows?
Thanks a lot

--
View this message in context: http://r.789695.n4.nabble.com/How-to-aggregate-combinations-tp4631867.html
Sent from the R help mailing list archive at Nabble.com.
#
Hello,

The problem can be solved using graph theory tools.

library(igraph)

edg <- read.table(text="
ID ID2
1    2
1    3
2    3
6    5
7    8
8    9
", header=TRUE)

(G <- graph(t(edg) - 1, directed=TRUE))  # igraph vertices are zero-based
V(G)$name <- 1:9

#V(G)$label <- V(G)$name
#plot(G, layout=layout.circle)

comp <- decompose.graph(G)
lapply(comp, function(x) colnames(get.adjacency(x)))

Hope this helps,

Rui Barradas

Em 30-05-2012 22:01, Oritteropus escreveu: