title: Calculating the size of an winning and losing streak
body: <p>I’m trying to calculate the <strong>size</strong> of an winning and losing streak, and this question is an follow-up of an <a href="https://stackoverflow.com/questions/4655848/calculating-a-consecutive-streak-in-data">earlier question</a> I had when I tried to calculate the length of an streak. </p>
  
  <p>This is what my data looks like:</p>
  
  <pre><code>&gt; subRes
     Instrument TradeResult.Currency.
  1         JPM                    -3
  2         JPM                   264
  3         JPM                   284
  4         JPM                    69
  5         JPM                   283
  6         JPM                  -219
  7         JPM                   -91
  8         JPM                   165
  9         JPM                   -35
  10        JPM                  -294
  11        KFT                    -8
  12        KFT                   -48
  13        KFT                   125
  14        KFT                  -150
  15        KFT                  -206
  16        KFT                   107
  17        KFT                   107
  18        KFT                    56
  19        KFT                   -26
  20        KFT                   189
  &gt; dput(subRes)
  structure(list(Instrument = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("JPM", 
  "KFT"), class = "factor"), TradeResult.Currency. = c(-3, 264, 
  284, 69, 283, -219, -91, 165, -35, -294, -8, -48, 125, -150, 
  -206, 107, 107, 56, -26, 189)), .Names = c("Instrument", "TradeResult.Currency."
  ), class = "data.frame", row.names = c(NA, 20L))
  </code></pre>
  
  <p><strong>My goal:</strong>
  I want to calculate the size of the longest winning and losing streak, for each instrument. So, for <code>JPM</code>  this would be the rows 2, 3, 4, and 5 from the data above, which gives the following <code>TradeResult.Currency.</code> values: 264 + 284 + 69 +283, for a total of 900. The size of the longest losing streak for JPM would be row 9 and 10, which give an total result of -329 (-35 +-294). For <code>KFT</code> the size of the longest winning streak is 270 (107 + 107 + 56, rows 16 till 18), and the size of the longest losing streak would be -356 (-150 + -206, rows 14 &amp; 15).</p>
  
  <p>The following function gives the <strong>correct</strong> size of the winning streak…</p>
  
  <pre><code>WinStreakSize &lt;- function(x){
      df.rle &lt;- ifelse(x &gt; 0, 1, 0)
      df.rle &lt;- rle(df.rle)
      wh &lt;- which(df.rle$lengths == max(df.rle$lengths))
      mx &lt;- df.rle$lengths[wh]
      suma &lt;- df.rle$lengths[1:wh]
      out &lt;- x[(sum(suma) - (suma[length(suma)] - 1)):sum(suma)]
      return(sum(out))
  }
  </code></pre>
  
  <p>.. resulting in:</p>
  
  <pre><code>&gt; with(subRes, tapply(TradeResult.Currency., Instrument, WinStreakSize)
  + )
  JPM KFT 
  900 270
  </code></pre>
  
  <p>However, I can’t seem to adept this function to display the size of the longest losing streak (so that it would output -329 for JPM and -356 for KFT), how stupid that may sound. I tried to change the function in numerous ways, stripped it and rebuild it, and I can't find the cause of it.</p>
  
  <p>Here’s what I mean (output from debugging the function, where the <code>x</code> values are the values for JPM after splitting <code>subRes</code>):</p>
  
  <pre><code>Browse[2]&gt;  ifelse(x &gt; 0, 1, 0)
   [1] 0 1 1 1 1 0 0 1 0 0
  Browse[2]&gt;  ifelse(x &lt; 0, 1, 0)
   [1] 1 0 0 0 0 1 1 0 1 1
  Browse[2]&gt; rle( ifelse(x &gt; 0, 1, 0))
  Run Length Encoding
    lengths: int [1:5] 1 4 2 1 2
    values : num [1:5] 0 1 0 1 0
  Browse[2]&gt; rle( ifelse(x &lt; 0, 1, 0))
  Run Length Encoding
    lengths: int [1:5] 1 4 2 1 2
    values : num [1:5] 1 0 1 0 1
  Browse[2]&gt; inverse.rle( ifelse(x &gt; 0, 1, 0))
  Error in x$lengths : $ operator is invalid for atomic vectors
  Browse[2]&gt; rle( !ifelse(x &lt; 0, 1, 0))
  Run Length Encoding
    lengths: int [1:5] 1 4 2 1 2
    values : logi [1:5] FALSE TRUE FALSE TRUE FALSE
  </code></pre>
  
  <p>So, changing the conditions in this function makes no difference in the output of the function. That would suggest I'm looking at the wrong part of the function for an solution, yet the <code>ifelse</code> statement is the first of the function. In other words, from line 1 and on, the function uses incorrect input despite changing the conditions.</p>
  
  <p>What obvious point am I missing?</p>