[R] making chains from pairs

David Winsemius dwinsemius at comcast.net
Sat Nov 9 09:55:25 CET 2013


On Nov 8, 2013, at 10:56 AM, Hermann Norpois 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

> second <- with(test, tapply(V2, V1, FUN=function(x) test[test$V2==x, ] ) )
Warning messages:
1: In test$V2 == x :
  longer object length is not a multiple of shorter object length
2: In test$V2 == x :
  longer object length is not a multiple of shorter object length
3: In test$V2 == x :
  longer object length is not a multiple of shorter object length

> third <- sapply(names(second) , function(df) c(df, second[[df]][ , "V2" ]) )
> third
$A
[1] "A" "B" "F" "G" "H"

$B
[1] "B" "F" "I" "F" "I"

$C
[1] "C" "F" "I" "K"

$D
[1] "D" "L" "M" "N"

$L
[1] "L" "O" "P"

> fourth <- sapply(names(third), function(d) unique( c(third[[d]], 
                                       unlist(third[ sapply( third[[d]], "[" ) ]) ) ) )
> fourth
$A
[1] "A" "B" "F" "G" "H" "I"

$B
[1] "B" "F" "I"

$C
[1] "C" "F" "I" "K"

$D
[1] "D" "L" "M" "N" "O" "P"

$L
[1] "L" "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.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list