title: Subtract two rows based on an index in R
body: <p>I have the following database. My intention is to subtract the scores of each name according to the category and Index. That is <strong>b - a</strong> of every name according the index.</p>
  <p>My database</p>
  <pre><code>dat &lt;- read.table(text=&quot;
  Name    Score   Index    Category
  Pepe        5      Al           a
  Pepe        1      Bl           b
  Juan        8      Cl           a
  Juan        5      Al           b
  Mikel       1      Cl           a
  Mikel       6      Bl           b
  &quot;, header=TRUE)
  </code></pre>
  <p>I want obtain this:</p>
  <p><a href="https://i.stack.imgur.com/LySCT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LySCT.png" alt="enter image description here" /></a></p>
  <p>Can you help me, please?</p>