title: Is there a better way to combine these dataframes and match values?
body: <p>I have two dataframes that I have combined using <code>left_join()</code></p>
  <p>data1 can be simplified to something like...</p>
  <pre><code>Date &lt;- as.Date(c('2011-7-26','2011-7-26','2010-11-1','2010-11-1','2009-5-10','2009-5-10','2008-3-25','2008-3-25','2007-3-14','2007-3-14'))
  Location &lt;- c(&quot;A&quot;,&quot;B&quot;,&quot;A&quot;,&quot;B&quot;,&quot;A&quot;,&quot;B&quot;,&quot;A&quot;,&quot;B&quot;,&quot;A&quot;,&quot;B&quot;)
  Result &lt;- sample(1:30, 10)
  data1 &lt;- data.frame(Date,Location,Result)
  </code></pre>
  <p>data2 can be simplified to something like...</p>
  <pre><code>Date &lt;- as.Date(c('2011-7-26','2009-5-10','2007-3-14'))
  Flow_A &lt;- c(6,2,9)
  Flow_B &lt;- c(10,11,25)
  data2 &lt;- data.frame(Date,Flow_A,Flow_B)
  </code></pre>
  <p>After combining by date, I have this</p>
  <pre><code>data3 &lt;- left_join(data2, data1, by = &quot;Date&quot;)
  
          Date Flow_A Flow_B Location Result
  1 2011-07-26      6     10        A     11
  2 2011-07-26      6     10        B     17
  3 2009-05-10      2     11        A      6
  4 2009-05-10      2     11        B     22
  5 2007-03-14      9     25        A     20
  6 2007-03-14      9     25        B      1
  </code></pre>
  <p>Each value in Result corresponds to a specific Location (A or B) and I want to attach the correct values for Flow (Flow_A or Flow_B) to that row according to location (i.e. combine columns Flow_A and Flow_B into one column 'Flow' with just the correct value). I have been able to do this using a combination of <code>mutate()</code>,<code>ifelse()</code>,<code>grepl()</code>,and very simple functions:</p>
  <pre><code>a &lt;- data3$Flow_A
  Choose_A &lt;- function(a) {
    return(a)}
  d &lt;- data3$Flow_B
  Choose_B &lt;- function(b) {
    return(b)} 
  
  data3 &lt;- mutate(data3, Flow =
               ifelse(grepl(&quot;A&quot;, Location), Choose_A(a), 
               ifelse(grepl(&quot;B&quot;, Location), Choose_B(b),NA)))
  
          Date Flow_A Flow_B Location Result Flow
  1 2011-07-26      6     10        A     11    6
  2 2011-07-26      6     10        B     17   10
  3 2009-05-10      2     11        A      6    2
  4 2009-05-10      2     11        B     22   11
  5 2007-03-14      9     25        A     20    9
  6 2007-03-14      9     25        B      1   25
  </code></pre>
  <p>But this seems rather clunky. Is there a better (more efficient) way to achieve this?</p>
  <p>Please excuse my ignorance - I'm still learning!
  Thanks!</p>