Search code examples
rdataframeleft-join

Match every row in a dataframe to each row in another dataframe in r


This might be a simple question, but I couldn't seem to find an obvious solution.

I have two data frames, df1 with 64 rows, and df2 with 662,343 rows. I join df1 to df2, where every row in df1 is mapped to each row in df2 so that I have 42,389,952 rows. df1 and df2 might look like this respectively:

df1: | Cancer | ID | |---------------------|------------------| | Sarcoma | 3435 | | Leukemia | 4465 |

df2:

Gene
TP53

new data frame :

Cancer ID Gene
Sarcoma 3435 TP53
Leukemia 4465 TP53

Thanks in advance for any help!


Solution

  • You may full_join without any matching column. So use by = character() in matching column argument. Demo

    df <- data.frame(X = c(1, 2))
    
    df2 <- data.frame(A = letters[1:3],
                      B = LETTERS[24:26])
    df
    #>   X
    #> 1 1
    #> 2 2
    df2
    #>   A B
    #> 1 a X
    #> 2 b Y
    #> 3 c Z
    
    dplyr::full_join(df2, df, by = character())
    #>   A B X
    #> 1 a X 1
    #> 2 a X 2
    #> 3 b Y 1
    #> 4 b Y 2
    #> 5 c Z 1
    #> 6 c Z 2
    

    Created on 2021-06-26 by the reprex package (v2.0.0)