Search code examples
rdatabasedataframepanelreplicate

repeat dataframe 1 for each id in data 2 in R


I have two dataframes:

df1 looks like with 2273780 other ID's:

id 
XIZ92
DA8J0

df2 looks like with 29435 other COMR :

COMR
9868
9875
1234

I want a df3, that replicate all COMR for each ID, like this :

id       COMR
XIZ92    9868
XIZ92    9875
XIZ92    1234
DA8J0    9868
DA8J0    9875
DA8J0    1234

I am sure that ther is an easy way but can't find my way around it. Thank you!


Solution

  • Two options:

    1. base::merge:

      merge(dat1, dat2, by = NULL)
      #      id COMR
      # 1 XIZ92 9868
      # 2 DA8J0 9868
      # 3 XIZ92 9875
      # 4 DA8J0 9875
      # 5 XIZ92 1234
      # 6 DA8J0 1234
      

      This works like this even if there are multiple columns in either frame.

      (This won't work with data.table objects, since data.table::merge.data.table requires non-null by arguments.)

    2. expand.grid:

      do.call(expand.grid, c(dat1, dat2))
      #      id COMR
      # 1 XIZ92 9868
      # 2 DA8J0 9868
      # 3 XIZ92 9875
      # 4 DA8J0 9875
      # 5 XIZ92 1234
      # 6 DA8J0 1234
      

      This works as long as there are exactly one column in each frame; if there are two or more columns, this explodes unnecessarily. Try it!

      dat2$ign <- 1
      nrow(do.call(expand.grid, c(dat1, dat2)))
      # [1] 18                             # expecting 6