I want to write a code to count and sum any positive and negative series of numbers.
Numbers are either positive or negative(no zero).
I have written codes with for
loops. Is there any creative alternative?
set.seed(100) x <- round(rnorm(20, sd = 0.02), 3)
x = [-0.01, 0.003, -0.002, 0.018, 0.002, 0.006, -0.012, 0.014, -0.017, -0.007, 0.002, 0.002, -0.004, 0.015, 0.002, -0.001, -0.008, 0.01, -0.018, 0.046]
sign_indicator <- ifelse(x > 0, 1,-1) number_of_sequence <- rep(NA, 20) n <- 1 for (i in 2:20) { if (sign_indicator[i] == sign_indicator[i - 1]) { n <- n + 1 } else{ n <- 1 } number_of_sequence[i] <- n } number_of_sequence[1] <- 1 ############################# summation <- rep(NA, 20) for (i in 1:20) { summation[i] <- sum(x[i:(i + 1 - number_of_sequence[i])]) }
sign_indicator = [1 if i > 0 else -1 for i in X] number_of_sequence = [1] N = 1 for i in range(1, len(sign_indicator)): if sign_indicator[i] == sign_indicator[i - 1]: N += 1 else: N = 1 number_of_sequence.append(N) ############################# summation = [] for i in range(len(X)): if number_of_sequence[i] == 1: summation.append(X[i]) else: summation.append(sum(X[(i + 1 - number_of_sequence[i]):(i + 1)]))
x n_of_sequence sum 1 -0.010 1 -0.010 2 0.003 1 0.003 3 -0.002 1 -0.002 4 0.018 1 0.018 5 0.002 2 0.020 6 0.006 3 0.026 7 -0.012 1 -0.012 8 0.014 1 0.014 9 -0.017 1 -0.017 10 -0.007 2 -0.024 11 0.002 1 0.002 12 0.002 2 0.004 13 -0.004 1 -0.004 14 0.015 1 0.015 15 0.002 2 0.017 16 -0.001 1 -0.001 17 -0.008 2 -0.009 18 0.010 1 0.010 19 -0.018 1 -0.018 20 0.046 1 0.046
To get the sum of a negative and a positive number, use the sign of the larger number and subtract. For example: (–7) + 4 = –3. 6 + (–9) = –3.
For example, the following formula will work just fine – =SUMIF(C2:C6,”>0″,B2). Since C2:C6 is already specified, you don't need to specify B2:B6. Only the first cell will do. The formula will automatically take the same size range starting from B2.
The other solutions look okay but you don't really need to use sophisticated language features or library functions for this simple problem.
result, prev = [], None for idx, cur in enumerate(x): if not prev or (prev > 0) != (cur > 0): n, summation = 1, cur else: n, summation = n + 1, summation + cur result.append((idx, cur, n, summation)) prev = cur
As you can see, you don't really need sign_indicator
list, two for-loops or range
function as in the snippet in the question section.
If you want index to start from 1, use enumerate(x, 1)
instead of enumerate(x)
To see the result, you can run the following code
for idx, num, length, summation in result: print(f"{idx:>2d} {num:.3f} {length:>2d} {summation:.3f}")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With