Merging different size data frames and repeating values

merge, r

Solution

You can do it simply with function `merge()`.

 merge(df1,df2,sort=FALSE)

   licto licfrom fisherID
1  17121   15470      160
2  17121   17121      160
3  17121   16307      160
4  16982   15350      760
5  16982   16982      760
6  16982   20319      760
7  16982   17182      760
8  16946   16727      770
9  16946   16946      770
10 16607   16605      406
11 16607   16607      406
12 15924   15924      106
13 16839   15399     2196
14 16839   15404     2196
15 16839   16739     2196
16 16839   16839     2196
17 16839   16842     2196
18 16839   16899     2196
19 16157   16157    17323
20 15399   15399     2441

Problem

I need to merge two different size data frames. The larger one (`df1`) has a column with several repeated values (`licto`), the shorter one (`df2`) has the column `licto`, but its values are not repeated. df2 also has a ID column. I need a new column in `df1` with the IDs in `df2`, repeated according the repeated values in `licto`. The example below probably make it clearer. ``` df1<-data.frame(licfrom=c(15470,16307,17121,15350,16982,17182,20319,16727,16946,16262,16605, 16607,15924,15399,15404,16739,16839,16842,16899,16157,15399), licto=c(17121,17121,17121,16982,16982,16982,16982,16946,16946,16262,16607, 16607,15924,16839,16839,16839,16839,16839,16839,16157,15399)) ``` . ``` df2<-data.frame(licto=c(17121,16982,16946,16607,15924,16839,16157,15399), fisherID=c(160,760,770,406,106,2196,17323,2441)) ``` My data frames look like this: ``` df1 df2 licfrom licto licto fisherID 15470 17121 17121 160 16307 17121 16982 760 17121 17121 16946 770 15350 16982 16262 947 16982 16982 16607 406 17182 16982 15924 106 20319 16982 16839 2196 16727 16946 16157 17323 16946 16946 15399 2441 16262 16262 16605 16607 16607 16607 15924 15924 15399 16839 15404 16839 16739 16839 16839 16839 16842 16839 16899 16839 16157 16157 15399 15399 ``` And my final data frame should be like this: ``` licfrom licto fisherID 15470 17121 160 16307 17121 160 17121 17121 160 15350 16982 760 16982 16982 760 17182 16982 760 20319 16982 760 16727 16946 770 16946 16946 770 16262 16262 947 16605 16607 406 16607 16607 406 15924 15924 106 15399 16839 2196 15404 16839 2196 16739 16839 2196 16839 16839 2196 16842 16839 2196 16899 16839 2196 16157 16157 17323 15399 15399 2441 ``` Any help will be appreciated, as I have spent several hour trying to merge as I need. I have used `merge` and `%in%` with no success. Thanks!

Original source