Identifier mapping is transitive

I have three sets of identifiers: "x", "y" and "z". I also have two, 2-column data frames, each of which maps one set of identifiers to another set of identifiers.

x2y = data.frame( x = c("A","A","B","B","C","D","E","F"),
                  y = c(1,2,1,2,3,4,4,5) )
y2z = data.frame( y = c(1,1,2,3,4,4,5,5,5),
                  z = c(1,2,3,3,6,7,6,7,8) )

This can be seen in the figure below. Notice that each arrow corresponds to one row in the data frame.

enter image description here

Question: How to use these two mappings (two data frames) to create a mapping from xto z(displayed to the right of the figure above). I think of it as a "transitive mapping": x to yand y to zgives x to z. The data frame that I would like ...

x2z = data.frame( x = c("A","A","A","B","B","B","C","D","D","E","E","F","F","F"),
                  z = c(1,2,3,1,2,3,3,6,7,6,7,6,7,8) )

: ~ 50 000 , . , . , R.

+4
1

:

merge(x2y, y2z)[c('x','z')]
##    x z
## 1  A 1
## 2  A 2
## 3  B 1
## 4  B 2
## 5  A 3
## 6  B 3
## 7  C 3
## 8  D 6
## 9  D 7
## 10 E 6
## 11 E 7
## 12 F 6
## 13 F 7
## 14 F 8

, , .

+2

All Articles