title: Matching two data tables (Vlookup, dplyr, match(), left_join) keeping number of rows
body: <p>I want to include the table "tech_distance" into my "first occurrences" table.
  The two data tables:</p>
  
  <pre><code>head(first_occurrences)
  # A tibble: 6 x 4
  # Groups:   Main, Second [6]
     year  Main Second  occurrence
     &lt;int&gt; &lt;chr&gt;  &lt;chr&gt;      &lt;int&gt;
  1  1991  C09D   C08F          1
  2  2002  A47C   A47D          1
  3  2002  G10K   H05K          1
  4  2004  G06G   C07K          1
  5  2015  B64F   B64D          1
  6  2015  H02G   B29C          1
  
  
  head(tech_distance)
  # A tibble: 6 x 2
      Main  tech_distance
     &lt;fctr&gt;         &lt;dbl&gt;
  1   C09D           0.3
  2   A47C           0.0
  3   G10K           0.5
  4   G06G           0.5
  5   B64F           0.0
  6   H02G           0.5 
  </code></pre>
  
  <p>This is the result I want to get: </p>
  
  <pre><code>head(first_occurrences)
     Main year Second occurrence tech_distance
   1 A01B 2004   E21B          1           0.7
   2 A01B 2004   E21B          1           0.5
   3 A01B 2004   E21B          1           0.7
   4 A01B 2004   E21B          1           0.5
   5 A01B 2004   E21B          1           0.5
   6 A01B 2004   E21B          1           1.0
  </code></pre>
  
  <p>I have used mutate in dplyr:</p>
  
  <pre><code>first_occurrences &lt;- data %&gt;% 
   select(year = X3,Main = X7,Second = X8) %&gt;% 
   group_by(Main,Second) %&gt;% 
   mutate(occurrence = n(), tech_distance) %&gt;% 
   filter(occurrence &gt;= 0, occurrence &lt;= 1, !(Main == Second)) 
  </code></pre>
  
  <p>But I get this error: </p>
  
  <pre><code> Error in mutate_impl(.data, dots) : 
   Column `tech_distance` must be length 24 (the group size) or one, not 2
  </code></pre>
  
  <p>So I tried using merge(): </p>
  
  <pre><code>first_occurrences &lt;- merge(first_occurrences, tech_distance, by.x = "Main", by.y = "Main", all.x=T)
  </code></pre>
  
  <p>This seems to work but I get an enormous number of rows (240,217 entries) </p>
  
  <pre><code> str(first_occurrences)
   'data.frame':  240217 obs. of  5 variables:
    $ Main         : chr  "A01B" "A01B" "A01B" "A01B" ...
    $ year         : int  2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 ...
    $ Second       : chr  "E21B" "E21B" "E21B" "E21B" ...
    $ occurrence   : int  1 1 1 1 1 1 1 1 1 1 ...
    $ tech_distance: num  0.7 0.5 0.7 0.5 0.5 1 0.5 0.7 0.3 0 ...
  </code></pre>
  
  <p>while the datasets before were: </p>
  
  <pre><code>str(first_occurrences)
  Classes ‘grouped_df’, ‘tbl_df’, ‘tbl’ and 'data.frame': 8015 obs. of  4 variables:
   $ year      : int  1991 2002 2002 2004 2015 2015 2015 2015 2015 2015 ...
   $ Main      : chr  "C09D" "A47C" "G10K" "G06G" ...
   $ Second    : chr  "C08F" "A47D" "H05K" "C07K" ...
   $ occurrence: int  1 1 1 1 1 1 1 1 1 1 ...
  
  str(tech_distance)
  Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   8015 obs. of  2 variables:
   $ Main         : Factor w/ 815 levels "A01B","A01C",..: 345 62 684 651 265 749 328 735 173 788 ...
   $ tech_distance: num  0.3 0 0.5 0.5 0 0.5 0.5 0 0.5 0.5 ...
  </code></pre>
  
  <p>Does anybody have any idea on how to merge two dataframes keeping the same number of rows?</p>