Quantcast
Channel: Count and summation of positive and negative number sequences - Stack Overflow
Browsing latest articles
Browse All 15 View Live
↧

Answer by Sinan Kurmus for Count and summation of positive and negative...

A simple python answer, ignores the 0 case: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]count =...

View Article


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

I think a loop would be easier to read, but just for fun, here's a solution in Python using recursion: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,...

View Article

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

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...

View Article

Answer by Adverse_Event for Count and summation of positive and negative...

Throwing my [r] answer in the hat, optimized for speed and works with any length of x (unlike the asker's which was hard coded for length 20):### data set.seed(100)x <- round(rnorm(20, sd = 0.02),...

View Article

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

In R, you could also do:# DATAset.seed(100)x <- round(rnorm(20, sd = 0.02), 3)library(data.table)dt <- data.table(x = x)# Create Positive or Negative variabledt$x_logical <- ifelse(dt$x >...

View Article


Answer by GoGonzo for Count and summation of positive and negative number...

I recommend R package runner for this kind of operations.streak_run calculates consecutive occurrence of the same value, and sum_run calculates sum in window which length is defined by k argument.Here...

View Article

Answer by user5538922 for Count and summation of positive and negative number...

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 = [], Nonefor idx, cur in enumerate(x): if not...

View Article

Answer by Prodipta Ghosh for Count and summation of positive and negative...

In Python, apart from defining a class to store the memory variables, you can use a closure to achieve the same.def run(): count = 0 last_sign = 0 def sign(i): return 1 if i > 0 else -1 def f(i):...

View Article


Answer by schot for Count and summation of positive and negative number...

Two different lazy solutions in Python, using the itertools module.Using itertools.groupby (and accumulate)from itertools import accumulate, groupbyresult = ( item for _, group in groupby(x, key=lambda...

View Article


Answer by Walter Tross for Count and summation of positive and negative...

As for Python, someone will come up with a solution using the pandas library. In the meantime, here is a simple proposal:class Combiner: def __init__(self): self.index = self.seq_index = self.summation...

View Article

Answer by Andrew Gustar for Count and summation of positive and negative...

Here is a simple tidyverse solution...library(tidyverse) #or just dplyr and tidyrset.seed(100)x <- round(rnorm(20, sd = 0.02), 3)df <- tibble(x = x) %>% mutate(seqno = cumsum(c(1,...

View Article

Answer by Allan Cameron for Count and summation of positive and negative...

Here's a simple non-looping function in R:count_and_sum <- function(x){ runs <- rle((x > 0) * 1)$lengths groups <- split(x, rep(1:length(runs), runs)) output <- function(group)...

View Article

Answer by Ameer for Count and summation of positive and negative number...

You can calculate the run lengths of each sign using rle from base to and do something like this. set.seed(0)z <- round(rnorm(20, sd = 0.02), 3)run_lengths <- rle(sign(z))$lengthsrun_lengths# [1]...

View Article


Answer by Ronak Shah for Count and summation of positive and negative number...

In R, you can use data.tables rleid to create groups with positive and negative series of number and then create a sequence of rows in each group and do a cumulative sum of the x values....

View Article

Count and summation of positive and negative number sequences

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...

View Article

Browsing latest articles
Browse All 15 View Live




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