title: Using mutate, if_else, and rowSums to create a new var based on condition
body: <p>I have a data frame  which is in long format. I have multiple cities. Each of the cities has information for each month and information also for each code (this code goes from 100 up to 1,000). My dataframe looks like this:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Code</th>
  <th>City</th>
  <th>month</th>
  <th>Data</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>100</td>
  <td>A</td>
  <td>10</td>
  <td>0</td>
  </tr>
  <tr>
  <td>100</td>
  <td>B</td>
  <td>12</td>
  <td>1</td>
  </tr>
  <tr>
  <td>100</td>
  <td>A</td>
  <td>10</td>
  <td>2</td>
  </tr>
  <tr>
  <td>100</td>
  <td>B</td>
  <td>12</td>
  <td>3</td>
  </tr>
  <tr>
  <td>100</td>
  <td>A</td>
  <td>10</td>
  <td>4</td>
  </tr>
  <tr>
  <td>100</td>
  <td>B</td>
  <td>12</td>
  <td>5</td>
  </tr>
  <tr>
  <td>200</td>
  <td>A</td>
  <td>10</td>
  <td>10</td>
  </tr>
  <tr>
  <td>200</td>
  <td>B</td>
  <td>12</td>
  <td>11</td>
  </tr>
  <tr>
  <td>200</td>
  <td>A</td>
  <td>10</td>
  <td>12</td>
  </tr>
  <tr>
  <td>200</td>
  <td>B</td>
  <td>12</td>
  <td>13</td>
  </tr>
  <tr>
  <td>200</td>
  <td>A</td>
  <td>10</td>
  <td>14</td>
  </tr>
  <tr>
  <td>200</td>
  <td>B</td>
  <td>12</td>
  <td>15</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I´m trying to create a new var that adds up the information in the <strong>Data</strong> variable foreach month when the variable <strong>Code</strong> is equal to 100. So for the 10th month I would have a result of 6, and for the 12th month I would have a result of 9:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Code</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>6</td>
  </tr>
  <tr>
  <td>9</td>
  </tr>
  <tr>
  <td>6</td>
  </tr>
  <tr>
  <td>9</td>
  </tr>
  <tr>
  <td>6</td>
  </tr>
  <tr>
  <td>9</td>
  </tr>
  <tr>
  <td>6</td>
  </tr>
  <tr>
  <td>9</td>
  </tr>
  <tr>
  <td>6</td>
  </tr>
  <tr>
  <td>9</td>
  </tr>
  <tr>
  <td>6</td>
  </tr>
  <tr>
  <td>9</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>For this I´m using <strong>dplyr</strong>:</p>
  <pre><code>df &lt;- df %&gt;%
  group_by(month) %&gt;% 
  mutate(newvar =case_when(Code==100 ~ as.integer(rowSums(select_(., &quot;Data&quot;), na.rm = TRUE))))       
  </code></pre>
  <p>However, I´m getting an error and I haven´t been able to create this new variable correctly. I know that an easier way would be using base R. But I want to use <strong>dplyr</strong>.</p>
  <p>Any help is really appreciate it!</p>