rjoindplyrmergetidyverse

merge two dataframes of different sizes without key


I have two dataframes with different columns and different row sizes

library(tidyverse)

tb1 <- tibble(id= 1:10,
              a= 1:10,
              b=11:20)

tb2 <- tibble(id= 1:5,
               q= 1:5,
               z= 11:15)

I can easily combine them by id key

full_join(tb1, tb2, by = "id")

.

# A tibble: 10 x 5
      id     a     b     q     z
   <int> <int> <int> <int> <int>
 1     1     1    11     1    11
 2     2     2    12     2    12
 3     3     3    13     3    13
 4     4     4    14     4    14
 5     5     5    15     5    15
 6     6     6    16    NA    NA
 7     7     7    17    NA    NA
 8     8     8    18    NA    NA
 9     9     9    19    NA    NA
10    10    10    20    NA    NA

How can I do the same if I don't have a column with a key and I don't want to create it in advance in each dataframe.

tb1 <- tibble(a= 1:10,
              b=11:20)

tb2 <- tibble(q= 1:5,
              z= 11:15)

is there any elegant solution? Not so bulky like this

tb1 %>%
  mutate(row = row_number()) %>%
  left_join(
    tb2 %>% mutate(row = row_number()),
    by = "row"
  ) %>%
  select(-row)

Solution

  • Try merge, but this is not recommended.

    merge(tb1, tb2, all=TRUE, by=0) # |> subset(select=-Row.names)
    

    Specifying by=0 merges on the rownames.

       Row.names  a  b  q  z
    1          1  1 11  1 11
    2         10 10 20 NA NA
    3          2  2 12  2 12
    4          3  3 13  3 13
    5          4  4 14  4 14
    6          5  5 15  5 15
    7          6  6 16 NA NA
    8          7  7 17 NA NA
    9          8  8 18 NA NA
    10         9  9 19 NA NA