[R] making chains from pairs

arun smartpink111 at yahoo.com
Sun Nov 10 04:32:39 CET 2013


Hi,

May be this helps.  But, the conditions are not very clear.
lst1 <- lapply(split(test,test$V1),function(x) unique(as.vector(t(x))))

indx <- unlist(lapply(lst1,function(x) which(names(lst1) %in% x)))
lst2 <- split(gsub("\\d+","",names(indx)),indx)
indx1 <- duplicated(lapply(lst2,`[`,1))|!(duplicated(lapply(lst2,`[`,1))|duplicated(lapply(lst2,`[`,1),fromLast=TRUE))
lapply(lst2[indx1],function(x) unique(unlist(lst1[x])))

A.K.





On Friday, November 8, 2013 10:50 PM, Hermann Norpois <hnorpois at gmail.com> wrote:
Hello,

having a data frame like test with pairs of characters I would like to
create chains. For instance from the pairs A/B and B/I you get the vector A
B I. It is like jumping from one pair to the next related pair. So for my
example test you should get:
A B F G H I
C F I K
D L M N O P


> test
   V1 V2
1   A  B
2   A  F
3   A  G
4   A  H
5   B  F
6   B  I
7   C  F
8   C  I
9   C  K
10  D  L
11  D  M
12  D  N
13  L  O
14  L  P

Thanks
Hermann

> dput (test)
structure(list(V1 = c("A", "A", "A", "A", "B", "B", "C", "C",
"C", "D", "D", "D", "L", "L"), V2 = c("B", "F", "G", "H", "F",
"I", "F", "I", "K", "L", "M", "N", "O", "P")), .Names = c("V1",
"V2"), row.names = c(NA, -14L), class = "data.frame")
>

    [[alternative HTML version deleted]]

______________________________________________
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