I have a dataframe A whose columns I want to match with the row.names of another dataframe B.
# A
v1 v2
X1 X3
X1 X5
X1 X15
X2 X3
X2 X4
...
# row.names of B (some values are duplicated)
row_names_B=c('X17', 'X1', 'X2', 'X15', 'X3', 'X3', 'X1', 'X5', 'X4', ...)
I want to match the columns of A
with the positions of row_names_B
, such that I can return a list of ALL positions in B for each row in A.
#my results:
v1_index v2_index
2 5 #matches X1 in pos 2, X3 in pos 5
2 6 #matches X1 in pos 2, X3 in pos 6
7 5 #matches X1 in pos 7, X3 in pos 5
7 6 #matches X1 in pos 7, X3 in pos 6
2 5 #matches X1 in pos 2, X3 in pos 8
7 5 #matches X1 in pos 7, X3 in pos 8
...
Note that I want to find all possible solutions.
I understand that this should be with some variant of match
or which
as given in this example, but I'm not sure how to do the explosion for each of the matches. The way I see it is by running it through for
loops, row by row, but perhaps there is a better way to do this?