title: R: Divide column Y by Z with absolute (positive) output, then sum output per unique value for column X
body: <p>for every <code>jobtask</code> I am trying to divide <code>value</code> by <code>weight</code> with two separate columns for calculating +1 values and the -1 values. For <code>outputnegative</code>, I would specifically need absolute (positive) values. </p>
  
  <p>Having this, I would then like to add 2 more columns again that per value for <code>occupation</code> (a/b/c) sums the <code>outputpositive</code> and <code>outputnegative</code>. I can't seem to figure it out, any help would be much appreciated!!! </p>
  
  <pre><code>occupation &lt;- c("a", "a", "a", "a", "b", "b", "b", "b", "c", "c", "c", "c")
  jobtask &lt;- c("1", "2", "3", "4","5", "6", "7", "8", "9", "10", "11", "12")
  value &lt;- c("1", "1", "0", "-1", "-1", "0", "-1", "1", "-1", "1", "0", "0")
  weight &lt;- c("95", "81", "97", "65", "43", "92", "89", "43", "58", "99", "35", "69")
  
  df &lt;- data.frame(occupation, jobtask, value, weight)
  </code></pre>
  
  <p>Desired output below:</p>
  
  <pre><code>   occupation jobtask value weight outputpos outputneg occupationpos occupationneg
  1           a       1     1     95      0.95                    1.76          0.65
  2           a       2     1     81      0.81                    1.76          0.65
  3           a       3     0     97                              1.76          0.65
  4           a       4    -1     65               -0.65          1.76          0.65
  5           b       5    -1     43               -0.43          0.43          1.22
  6           b       6     0     92                              0.43          1.22
  7           b       7    -1     89               -0.89          0.43          1.22
  8           b       8     1     43      0.43                    0.43          1.22
  9           c       9    -1     58               -0.58          0.99          0.58
  10          c      10     1     99      0.99                    0.99          0.58
  11          c      11     0     35                              0.99          0.58
  12          c      12     0     69                              0.99          0.58
  </code></pre>
  
  <p>Extra addition to fix multiple years. In the df every row is one year, so the "sum" for that specific occupation is now 5.42, while it should be 0.95 + 0.81 = 1.76 like it was in the example above. Every <code>jobtask</code> has different number of <code>year</code>, so cannot simply divide by one number to account for the number of years. Any suggestions?</p>
  
  <pre><code> occupation jobtask year value weight outputpos outputneg occupationpos occupationneg
  1          a       1 2015     1     95      0.95                    5.42              
  2          a       1 2016     1     95      0.95                    5.42             
  3          a       1 2017     1     95      0.95                    5.42             
  4          a       1 2018     1     95      0.95                    5.42             
  5          a       2 2015     1     81      0.81                    5.42             
  6          a       2 2016     1     81      0.81                    5.42             ```
  
  </code></pre>