title: Conditional summing (R)
body: <p>I'm trying to create a conditional sum, in order to calculate an average. The idea is that an function (or an apply statement) checks if an certain value is true (for example x > 0), then sums all the values of x that where higher than zero. The last step would be to divide this sum by the number of instances which are greater than zero. Searching for conditonal sum(ming) didn't gave me usable information.</p>
  
  <p>This is a part of the data:</p>
  
  <pre><code>&gt; tmpData
     Instrument TradeResult.Currency.
  1         JPM                    -3
  2         JPM                   264
  3         JPM                   284
  4         JPM                    69
  5         JPM                   283
  11        KFT                    -8
  12        KFT                   -48
  13        KFT                   125
  14        KFT                  -150
  15        KFT                  -206
  16        KFT                   107
  </code></pre>
  
  <p>Of the functions that I've tried, the following holds the most promise:</p>
  
  <pre><code>avgProfit &lt;- function(x) {
      ifelse(x &gt; 0,
      sum(x) / length(which(x &gt; 0)),
      return(0))
      }
  </code></pre>
  
  <p>However, the output of this function is 0:</p>
  
  <pre><code>&gt; with(tmpData, tapply(TradeResult.Currency., Instrument, avgProfit))
  JPM KFT 
    0   0     
  &gt; avgProfit(tmpData$TradeResult.Currency.)
  [1] 0
  &gt; x
   [1] 1 1 2 1 2 3 3 3 4 4
  </code></pre>
  
  <p>(The values should be 225 for JPM (total of 900 divided by 4 instances which where greater than zero) and 116 for KFT)</p>
  
  <p>Even though I calculate the sum of x (which, if I understand correctly, should be the sum of the individual values in the data.frame) in the function, the output of the variable 'x' puzzles me. I can't find where these 1,2,3 and fours are coming from.</p>
  
  <p>How can I calculate an conditional sum? Besides, do I need to use an function or am I making it too complicated (perhaps there is an build-in R function for this which I overlooked?)</p>
  
  <p>Any thoughts are more than welcome,</p>
  
  <p>Regards,</p>