Cross two dataframes

Asked

Viewed 45 times

1

Considering 2 dataframes of different sizes: df1 consisting of 2 sets of data pairs (origin, destination) with the same origin (one set aggregates vertices originating in A and another aggregates vertices originating in E) and df2 consisting of pairs of vertices (x y,):

df1 = data.frame(
    origem = c("A","A", "A", "A", "A", "A", "A", "E", "E", "E", "E"), 
    destino=c("A","B","C","F", "J", "H", "G", "E", "D", "G", "F"))
df2 = data.frame(
    x = c("A","A", "E", "J", "C", "C"), 
    y = c("A","C","G","B", "F", "H"))

For each set defined in df1 with the same origin (p.e. GA for the destination vertices originating in A) I intend to identify all destination vertices forming pairs (x,y) of vertices belonging to that same group. For example, the group originating in A (GA) presents the following set of destination vertices {A, B, C, F, J, H,G } and the vertices forming pairs (x, y) consisting only of vertices of that group are: A, B, C, F, H ( J does not exist in column y of df2, G exists in column y but is associated with x=E and E is not vertex of the set GA). The goal would be to obtain a table with the following information where 1 marks the destination vertices in the above conditions:

   origem destino flag
1       A       A    1
2       A       B    1
3       A       C    1
4       A       F    1
5       A       J    0
6       A       H    1
7       A       G    0
8       E       E    0
9       E       D    0
10      E       G    1
11      E       F    0

Can anyone help? Obg

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.