Quantcast
Channel: Count and summation of positive and negative number sequences - Stack Overflow
Viewing all articles
Browse latest Browse all 15

Answer by lotus for Count and summation of positive and negative number sequences

$
0
0

Here's another base R approach:

data.frame(x,           n = sequence(rle(sign(x))$lengths),           sum = Reduce(function(x, y) if (sign(x) == sign(y)) x + y else y, x, accumulate = TRUE))        x n    sum1  -0.010 1 -0.0102   0.003 1  0.0033  -0.002 1 -0.0024   0.018 1  0.0185   0.002 2  0.0206   0.006 3  0.0267  -0.012 1 -0.0128   0.014 1  0.0149  -0.017 1 -0.01710 -0.007 2 -0.02411  0.002 1  0.00212  0.002 2  0.00413 -0.004 1 -0.00414  0.015 1  0.01515  0.002 2  0.01716 -0.001 1 -0.00117 -0.008 2 -0.00918  0.010 1  0.01019 -0.018 1 -0.01820  0.046 1  0.046

Viewing all articles
Browse latest Browse all 15

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>