Intersecting multiple columns between two data frames

user1701545

I have two data frames with 2 columns in each. For example:

df.1 = data.frame(col.1 = c("a","a","a","a","b","b","b","c","c","d"), col.2 = c("b","c","d","e","c","d","e","d","e","e"))
df.2 = data.frame(col.1 = c("b","b","b","a","a","e"), col.2 = c("a","c","e","c","e","c"))

and I'm looking for an efficient way to look up the row index in df.2 of every col.1 col.2 row pair of df.1. Note that a row pair in df.1 may appear in df.2 in reverse order (for example df.1[1,], which is "a","b" appears in df.2[1,] as "b","a"). That doesn't matter to me. In other words, as long as a row pair in df.1 appears in any order in df.2 I want its row index in df.2, otherwise it should return NA. One more note, row pairs in both data frames are unique - meaning each row pair appears only once.

So for these two data frames the return vector would be:

c(1,4,NA,5,2,NA,3,NA,6,NA)
npjc

Maybe something using dplyr package:

first make the reference frame

  • use row_number() to number as per row index efficiently.
  • use select to "flip" the column vars.

two halves:

df_ref_top <- df.2 %>% mutate(n=row_number())
df_ref_btm <- df.2 %>% select(col.1=col.2, col.2=col.1) %>% mutate(n=row_number())

then bind together:

df_ref <- rbind(df_ref_top,df_ref_btm)

Left join and select vector:

gives to get your answer

left_join(df.1,df_ref)$n

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Product between two data.frames columns

From Dev

Merge two data frames considering a range match between key columns

From Java

pandas: merge (join) two data frames on multiple columns

From Dev

match two data.frames based on multiple columns

From Dev

pandas: merge (join) two data frames on multiple columns

From Dev

Subset rows of multiple data frames based on variable values on two columns

From Dev

Matching two columns of two different data frames

From Dev

Pandas: Merge two data frames and keep non-intersecting data from a single data frame

From Dev

Merge two data frames and select specific columns

From Dev

R: Merge two data frames by common columns

From Dev

Concatenating two data frames on large no. of columns

From Dev

Perform full Merge between the columns of two data frames, based on starting alphabet

From Dev

Combining multiple data frames with different number of columns

From Dev

Python: Intersecting multiple List columns

From Dev

Number of intersections dependent on order of intersecting data frames

From Dev

Replace values between two data frames in R

From Dev

Comparing the difference in values between two data frames

From Dev

R - subtracting multiple columns from multiple columns with 2 data frames

From Dev

Copy data from one sheet that lies between two time frames, to multiple other sheets

From Dev

Search multiple fields in two data Frames in R

From Dev

Merge two data frames on multiple values

From Dev

Merging rows within a data frame and merging columns between data frames

From Dev

Merging rows within a data frame and merging columns between data frames

From Dev

Need to merge multiple data frames by multiple common columns

From Dev

Need to merge multiple data frames by multiple common columns

From Dev

Compare data between two columns

From Dev

How to pass columns in two data frames to Haversine Function?

From Dev

Merging two data frames with different sizes by matching their columns

From Dev

How to join data.frames based on two columns

Related Related

  1. 1

    Product between two data.frames columns

  2. 2

    Merge two data frames considering a range match between key columns

  3. 3

    pandas: merge (join) two data frames on multiple columns

  4. 4

    match two data.frames based on multiple columns

  5. 5

    pandas: merge (join) two data frames on multiple columns

  6. 6

    Subset rows of multiple data frames based on variable values on two columns

  7. 7

    Matching two columns of two different data frames

  8. 8

    Pandas: Merge two data frames and keep non-intersecting data from a single data frame

  9. 9

    Merge two data frames and select specific columns

  10. 10

    R: Merge two data frames by common columns

  11. 11

    Concatenating two data frames on large no. of columns

  12. 12

    Perform full Merge between the columns of two data frames, based on starting alphabet

  13. 13

    Combining multiple data frames with different number of columns

  14. 14

    Python: Intersecting multiple List columns

  15. 15

    Number of intersections dependent on order of intersecting data frames

  16. 16

    Replace values between two data frames in R

  17. 17

    Comparing the difference in values between two data frames

  18. 18

    R - subtracting multiple columns from multiple columns with 2 data frames

  19. 19

    Copy data from one sheet that lies between two time frames, to multiple other sheets

  20. 20

    Search multiple fields in two data Frames in R

  21. 21

    Merge two data frames on multiple values

  22. 22

    Merging rows within a data frame and merging columns between data frames

  23. 23

    Merging rows within a data frame and merging columns between data frames

  24. 24

    Need to merge multiple data frames by multiple common columns

  25. 25

    Need to merge multiple data frames by multiple common columns

  26. 26

    Compare data between two columns

  27. 27

    How to pass columns in two data frames to Haversine Function?

  28. 28

    Merging two data frames with different sizes by matching their columns

  29. 29

    How to join data.frames based on two columns

HotTag

Archive