title: R loop to subset information from a few subsequent rows and create new column
body: <p>I am still relatively new to R and I got this challenge.</p>
  <p>I have a table (code below) in which the first column consists of names of managers and projects they have. Always after the name of manager (e.g. 'Tom Jones') his projects (e.g. '019200 [ytb] ppp') and other activities (e.g. 'Risk Management') are listed . Other columns represent capacity hours (<code>capacity</code>) and actual working hours (<code>actual_hours</code>) that manager spent on projects.</p>
  <p>[Updated data frame]</p>
  <pre><code>df &lt;- data.frame('Name'=c('Tom Jones', '0192000 [ytb] ppp', 'I-2020020 [BBB] Adm Valid', 'TENT DATZ 20204001 [ASS]','Risk Management', 'Andrea Jose Casteno','2040044 [Amaas] HHHHJ', '0291111 [inezzz] plmi', 'Training', 'Gerard van der Dike', '20200222+20203333 [Ttt ID/ZZ] HHH SSS DD +Dpp', 'Other' , 'R-20204444 [Res] Phhh Top SAA', 'Bill Gatea', 'Leave','2019xxxx [zzz] Aap ZZ'), 'capacity' = c(40,0,0,0,0,36,0,0,0,40,0,0,0,38,0,0), 'actual_hours' = c(44,12,2,20,10,40,30,0,10,43,20,10,13,38,20,18))
  </code></pre>
  <p>List of managers:</p>
  <pre><code>manager_names= c('Tom Jones', 'Andrea Jose Casteno', 'Gerard van der Dike', 'Bill Gatea')
  </code></pre>
  <p>I need to transform this table into the below output (picture below). For each manager that worked more than his capacity (<code>actual_hours</code> &gt; <code>capacity</code>), I need to create a row in the new table. First column should be manager name. Second and third column show his <code>capacity</code> and <code>actual_hours</code>. Forth column should present short project names that manger worked on (exclude projects with 0 == <code>actual_hours</code>). The short project names need to be subtracted from the <code>Name</code> column - they are in the square brackets (e.g. BBB)</p>
  <p><a href="https://i.stack.imgur.com/76cmf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/76cmf.png" alt="output" /></a></p>
  <p>For now, I tried the below lines but I am stuck with how to subtract the information about projects (fourth column in the output). Probably a loop using the list of manager names is needed.</p>
  <pre><code>z=(which(df$actual_hours &gt; df$capacity))
  
  output_df=df[z,c(&quot;Name&quot;,&quot;capacity&quot;,&quot;actual_hours&quot;)]
  
  y=which(output_df$capacity==0, arr.ind=TRUE)
  
  newdf &lt;- output_df[-y, ]
  </code></pre>
  <p>Thanks in advance for your help !</p>