cumsum

Compute the cumulative sum of a list until a zero appears

三世轮回 提交于 2019-12-20 10:05:20
问题 I have a (long) list in which zeros and ones appear at random: list_a = [1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1] I want to get the list_b sum of the list up to where 0 appears where 0 appears, retain 0 in the list list_b = [1, 2, 3, 0, 1, 2, 0, 1, 0, 1, 2, 3] I can implement this as follows: list_b = [] for i, x in enumerate(list_a): if x == 0: list_b.append(x) else: sum_value = 0 for j in list_a[i::-1]: if j != 0: sum_value += j else: break list_b.append(sum_value) print(list_b) but the actual

iterative cumsum where sum determines the next position to be added

谁说我不能喝 提交于 2019-12-20 07:46:47
问题 I have a data.table as follows set.seed(5) x <- data.table(x=sample(1:20,15)) > x x 1: 5 2: 14 3: 17 4: 20 5: 2 6: 11 7: 8 8: 15 9: 12 10: 16 11: 3 12: 18 13: 10 14: 4 15: 13 and I would like to start at 1 and cumulate values iteratively such that the value of cumsum() determines the next number to be added to the sum. In the example I want to add the first value of x , here 5, then jump to value number 5 and add that, here 2, then jump to value number 5+2=7 , here 8, then value number 5+2+8

Shiny: calculate cumsum based on dygraphs' RangeSelector

蓝咒 提交于 2019-12-20 04:55:30
问题 I'm building a shiny app where I want to plot a dataset with one of the variables being a cumulative sum of another variable. The latter needs to be re-calculated every time the start date of dygraphs ' dyRangeSelector changes. Below is a basic code without cumsum calculations. Commented out code is what I tried, with no success. library(shinydashboard) library(stringr) library(zoo) library(dplyr) library(dygraphs) ui <-dashboardPage( dashboardHeader(), dashboardSidebar(), dashboardBody(

cumsum along row of data.frame with NA in R

主宰稳场 提交于 2019-12-20 03:11:30
问题 The hypothetical case is that there exist NA in a data.frame > a <- c(1:5, NA, 7:10) > b <- 1:10 > c <- 1:10 > > data <- data.frame(a,b,c) > data a b c 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5 5 6 NA 6 6 7 7 7 7 8 8 8 8 9 9 9 9 10 10 10 10 > data <- data.frame(a,b,c) > data.frame(t(apply(data,1,cumsum))) a b c 1 1 2 3 2 2 4 6 3 3 6 9 4 4 8 12 5 5 10 15 6 NA NA NA 7 7 14 21 8 8 16 24 9 9 18 27 10 10 20 30 My desired result is a b c 1 1 2 3 2 2 4 6 3 3 6 9 4 4 8 12 5 5 10 15 6 0 6 12 7 7 14 21 8 8

Matlab, alternative to creating an extra vector?

倾然丶 夕夏残阳落幕 提交于 2019-12-19 10:17:47
问题 If I have this I get an error sum(vector) == cumsum(vector)(length(vector)) >> Error: ()-indexing must appear last in an index expression. I know I can just do: Vec1 = cumsum(mat); sum(mat) == Vec1(length(mat)) which will return a logical 1. Is there an alternative to get everything on a single line? 回答1: Well, if you are absolutely determined to do it in one line, sum(vec) == subsref(cumsum(vec),struct('type','()','subs',{{numel(vec)}})) But this is a borderline abuse of subsref, which is

Cumulative sum in a matrix

送分小仙女□ 提交于 2019-12-19 05:15:11
问题 I have a matrix like A= [ 1 2 4 2 3 1 3 1 2 ] and I would like to calculate its cumulative sum by row and by column, that is, I want the result to be B = [ 1 3 7 3 8 13 6 12 19 ] Any ideas of how to make this in R in a fast way? (Possibly using the function cumsum) (I have huge matrices) Thanks! 回答1: A one-liner: t(apply(apply(A, 2, cumsum)), 1, cumsum)) The underlying observation is that you can first compute the cumulative sums over the columns and then the cumulative sum of this matrix

R: cumulative sum over rolling date range

混江龙づ霸主 提交于 2019-12-18 13:53:39
问题 In R, how can I calculate cumsum for a defined time period prior to the row being calculate? Prefer dplyr if possible. For example, if the period was 10 days, then the function would achieve cum_rolling10: date value cumsum cum_rolling10 1/01/2000 9 9 9 2/01/2000 1 10 10 5/01/2000 9 19 19 6/01/2000 3 22 22 7/01/2000 4 26 26 8/01/2000 3 29 29 13/01/2000 10 39 29 14/01/2000 9 48 38 18/01/2000 2 50 21 19/01/2000 9 59 30 21/01/2000 8 67 38 25/01/2000 5 72 24 26/01/2000 1 73 25 30/01/2000 6 79 20

Python pandas cumsum with reset everytime there is a 0 [duplicate]

不羁的心 提交于 2019-12-18 06:59:55
问题 This question already has answers here : Cumsum reset at NaN (4 answers) Closed last year . I have a matrix with 0s and 1s, and want to do a cumsum on each column that resets to 0 whenever a zero is observed. For example, if we have the following: df = pd.DataFrame([[0,1],[1,1],[0,1],[1,0],[1,1],[0,1]],columns = ['a','b']) print(df) a b 0 0 1 1 1 1 2 0 1 3 1 0 4 1 1 5 0 1 The result I desire is: print(df) a b 0 0 1 1 1 2 2 0 3 3 1 0 4 2 1 5 0 2 However, when I try df.cumsum() * df , I am able

Cumulative sum across columns instead of rows

大兔子大兔子 提交于 2019-12-18 05:06:26
问题 I have a data.table dt as follows. df <- data.frame(t1 = rep(0,5), t3 = c(12, 5, 8,9, 5), t7= c(25, 48, 7, 9, 14)) dt <- setDT(df) dt t1 t3 t7 1: 0 12 25 2: 0 5 48 3: 0 8 7 4: 0 9 9 5: 0 5 14 I want to get the cumulative sums across the columns. I am only getting it across the rows. How to do this in data.table . dt[, 1:3 := cumsum(dt)] dt t1 t3 t7 1: 0 12 25 2: 0 17 73 3: 0 25 80 4: 0 34 89 5: 0 39 103 The desired output is as follows: dt t1 t3 t7 1: 0 12 37 2: 0 5 53 3: 0 8 15 4: 0 9 18 5:

Conditional cumsum with reset

十年热恋 提交于 2019-12-17 18:19:34
问题 I have a data frame, the data frame is already sorted as needed, but now I will like to "slice it" in groups. This groups should have a max cumulative value of 10. When the cumulative value is > 10, it should reset the cumulative sum and start over again library(dplyr) id <- sample(1:15) order <- 1:15 value <- c(4, 5, 7, 3, 8, 1, 2, 5, 3, 6, 2, 6, 3, 1, 4) df <- data.frame(id, order, value) df This is the output I'm looking for(I did it "manually") cumsum_10 <- c(4, 9, 7, 10, 8, 9, 2, 7, 10,