title: How do I merge partial data and format it in R?
body: <p>So I have this R file:</p>
  <pre><code># Open CSV file
  df &lt;- read.table(&quot;myfile.txt&quot;, header = FALSE, sep = &quot;:&quot;, dec = &quot;-&quot;)
  
  # Delete the first column
  df &lt;- df[-1]
  
  # Delete every 3rd and 4th columns
  i1 &lt;- rep(seq(3, ncol(df), 4) , each = 2) + 0:1
  df &lt;- df[,-i1]
  
  # Remove extra text from columns
  df[] &lt;- lapply(df, gsub, pattern = &quot;,title&quot;, replacement = &quot;&quot;, fixed = TRUE)
  df[] &lt;- lapply(df, gsub, pattern = &quot;,url&quot;, replacement = &quot;&quot;, fixed = TRUE)
  
  # Swap columns around so title comes before ID
  df &lt;- data.frame(new_col = paste0('&quot;', df[seq(2, ncol(df), 2)], '#', df[seq(1, ncol(df)-1, 2)], '&quot;', ','))
  
  # Write to file
  write.table(df, file = &quot;result.lua&quot;, sep = &quot;#&quot;, quote = FALSE, row.names = FALSE, col.names = FALSE)
  </code></pre>
  <p>And this is myfile.txt:</p>
  <pre><code>(new AudioControls()).init([{&quot;id&quot;:53201,&quot;title&quot;:&quot;bbc-two intro&quot;,&quot;url&quot;:&quot;https:\/\/wow.bbc.com\/bbc-ids\/classic\/enus\/209\/53201&quot;,&quot;type&quot;:&quot;audio\/mpeg&quot;},{&quot;id&quot;:53227,&quot;title&quot;:&quot;bbc - demo04&quot;,&quot;url&quot;:&quot;https:\/\/wow.bbc.com\/bbc-ids\/classic\/enus\/235\/53227&quot;,&quot;type&quot;:&quot;audio\/mpeg&quot;},{&quot;id&quot;:53301,&quot;title&quot;:&quot;bbc-horizon quality&quot;,&quot;url&quot;:&quot;https:\/\/wow.bbc.com\/bbc-ids\/classic\/enus\/53\/53301&quot;,&quot;type&quot;:&quot;audio\/mpeg&quot;}], WH.ge('temporal-fixation'), {});
  </code></pre>
  <p>And when run, this is what it produces (result.txt):</p>
  <pre><code>&quot;bbc-two intro#53201&quot;,
  &quot;bbc - demo04#53227&quot;,
  &quot;bbc-horizon quality#53301&quot;,
  </code></pre>
  <p>So it takes the numeric ID and title from myfile.txt and it puts it in the format title#id.  All good so far.</p>
  <p>However, what I need to do is change the titles.  So I have another file called base.txt.  This is base.txt.</p>
  <pre><code>53201;streets/faculties/travelguide.txt
  121597;zones/sunburn/fatality_02.txt
  53227;stems/futures/holiday.txt
  121599;quiz/question/functional.mp3
  121600;alone/future/suicide.dat
  121601;crossroads/valley/melbourne.dec
  </code></pre>
  <p>So what I want is for the IDs that exist in both myfile.txt and base.txt to be merged somewhat so that the output shows the ID that exists in both but the title is only from base.txt.</p>
  <p>So the desired output would be this:</p>
  <pre><code>&quot;streets/faculties/travelguide.txt#53201&quot;,
  &quot;stems/futures/holiday.txt#53227&quot;,
  </code></pre>
  <p>With the quotes and commas as shown.  Note that only the IDs that exist in both myfile.txt and base.txt should be shown in the new table.</p>
  <p>Can someone tell me how to achieve this?</p>
  <p>Thank you.</p>