[R] How to aggregate combinations

Rui Barradas ruipbarradas at sapo.pt
Thu May 31 00:07:47 CEST 2012


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:
> 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.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list