title: Create new column in dataframe based on another and matching to another dataset in R
body: <p>I've the following data frame and I would like to create a new column based on the following conditions:</p>
  <ol>
  <li>If Price.of.Books is BLANK, R will look up to the matching data frame based on the value in Count.of.Books..Match.</li>
  <li>If Price.of.Books is not BLANK, it will retain the value</li>
  </ol>
  <p>In Excel, it can be done with ifelse() with a vlookup() within the ifelse() function. Is there something similar that can be done in R? So that it will work for larger datasets?</p>
  <p>Dataframe</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Index</th>
  <th>Grouping</th>
  <th>Count.of.Books..Match.</th>
  <th>Price.of.Books</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>i1</td>
  <td>A</td>
  <td>1</td>
  <td>BLANK</td>
  </tr>
  <tr>
  <td>i2</td>
  <td>A</td>
  <td>2</td>
  <td>BLANK</td>
  </tr>
  <tr>
  <td>i3</td>
  <td>B</td>
  <td>2</td>
  <td>12</td>
  </tr>
  <tr>
  <td>i4</td>
  <td>B</td>
  <td>6</td>
  <td>BLANK</td>
  </tr>
  <tr>
  <td>i5</td>
  <td>C</td>
  <td>4</td>
  <td>10</td>
  </tr>
  <tr>
  <td>i6</td>
  <td>C</td>
  <td>1</td>
  <td>11.5</td>
  </tr>
  <tr>
  <td>i7</td>
  <td>D</td>
  <td>3</td>
  <td>8.5</td>
  </tr>
  <tr>
  <td>i8</td>
  <td>D</td>
  <td>6</td>
  <td>BLANK</td>
  </tr>
  <tr>
  <td>i9</td>
  <td>E</td>
  <td>4</td>
  <td>BLANK</td>
  </tr>
  </tbody>
  </table>
  </div>
  <pre><code>df = structure(list(Index = structure(1:9, .Label = c(&quot;I1&quot;, &quot;I2&quot;, 
                      &quot;I3&quot;, &quot;I4&quot;, &quot;I5&quot;, &quot;I6&quot;, &quot;I7&quot;, &quot;I8&quot;, &quot;I9&quot;), class = &quot;factor&quot;), 
                   Grouping = structure(c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L
                   ), .Label = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;, &quot;E&quot;), class = &quot;factor&quot;), 
                   Count.of.Books..Match. = c(1L, 2L, 2L, 6L, 4L, 1L, 3L, 6L, 
                    4L), Price.of.Books = structure(c(5L, 5L, 3L, 5L, 1L, 2L, 
                     4L, 5L, 5L), .Label = c(&quot;10&quot;, &quot;11.5&quot;, &quot;12&quot;, &quot;8.5&quot;, &quot;BLANK&quot;
                     ), class = &quot;factor&quot;)), row.names = c(NA, -9L), class = &quot;data.frame&quot;)
  </code></pre>
  <p>Matching Data Frame</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Match</th>
  <th>Price.of.Books</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>6</td>
  </tr>
  <tr>
  <td>2</td>
  <td>7</td>
  </tr>
  <tr>
  <td>3</td>
  <td>8</td>
  </tr>
  <tr>
  <td>4</td>
  <td>9.5</td>
  </tr>
  <tr>
  <td>5</td>
  <td>12</td>
  </tr>
  <tr>
  <td>6</td>
  <td>13</td>
  </tr>
  </tbody>
  </table>
  </div>
  <pre><code>match = structure(list(Match = 1:6, Price.of.Books = c(6, 7, 8, 9.5, 12, 13)), class = &quot;data.frame&quot;, row.names = c(NA, -6L))
  </code></pre>
  <p>The outcome should be the following:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Index</th>
  <th>Grouping</th>
  <th>Count.of.Books..Match.</th>
  <th>Price.of.Books</th>
  <th>New Column</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>i1</td>
  <td>A</td>
  <td>1</td>
  <td>BLANK</td>
  <td>6</td>
  </tr>
  <tr>
  <td>i2</td>
  <td>A</td>
  <td>2</td>
  <td>BLANK</td>
  <td>7</td>
  </tr>
  <tr>
  <td>i3</td>
  <td>B</td>
  <td>2</td>
  <td>12</td>
  <td>12</td>
  </tr>
  <tr>
  <td>i4</td>
  <td>B</td>
  <td>6</td>
  <td>BLANK</td>
  <td>13</td>
  </tr>
  <tr>
  <td>i5</td>
  <td>C</td>
  <td>4</td>
  <td>10</td>
  <td>10</td>
  </tr>
  <tr>
  <td>i6</td>
  <td>C</td>
  <td>1</td>
  <td>11.5</td>
  <td>11.5</td>
  </tr>
  <tr>
  <td>i7</td>
  <td>D</td>
  <td>3</td>
  <td>8.5</td>
  <td>8.5</td>
  </tr>
  <tr>
  <td>i8</td>
  <td>D</td>
  <td>6</td>
  <td>BLANK</td>
  <td>13</td>
  </tr>
  <tr>
  <td>i9</td>
  <td>E</td>
  <td>4</td>
  <td>BLANK</td>
  <td>9.5</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Thanks in advance!</p>