## Cumulative sum of consecutive values

I have a simple dataset with US NBER recessions that are currently coded as dummy variables. I would like to sequentially label each recession. For example, in the below table, I want the recession column to read "Recession 1", "No Recession", "Recession 2", and so on, thereby categorizing each recession.

Date Recession 1949-06-30 1 1949-09-30 1 1949-12-31 1 1950-03-31 0 1950-06-30 0 1953-09-30 1 1953-12-31 1

You can use `rle`

to count consecutive runs of `1`

and repeat (`rep`

) them corresponding number of times (`lengths`

)

foo <- with(rle(input$Recession), rep(cumsum(values) * values, lengths)) ifelse(foo == 0, "No Recession", paste("Recession", foo))

Input:

structure(list(Date = c("1949-06-30", "1949-09-30", "1949-12-31", "1950-03-31", "1950-06-30", "1953-09-30", "1953-12-31"), Recession = c(1L, 1L, 1L, 0L, 0L, 1L, 1L)), row.names = c(NA, -7L), class = "data.frame")

**Cumulative sum of consecutive positive or negative numbers ,** With data being data = {-1, 2, 3, 4, 2, 1, -2, -5, 3, 1, 2, 1, -1, -1, 1, 2, 4, -1, 1, 1, -1, -2, 1};. as in the OP, then do. Accumulate /@ SplitBy[data, Sign] Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more . Cumulative sum based on consecutive values. Ask Question Asked 6 years, 1 month ago. Active 6 years, 1 month ago. Viewed 2k times 3.

Here's a tidyverse approach:

- Use
`lag`

to figure out if the status of recession changed - Use
`&`

and`cumsum`

to figure out if it changed from no recession to recession - Replace all the rows that should be "Recession" with "No Recession" with
`if_else`

library(tidyverse) df <- read_table2( "Date Recession 1949-06-30 1 1949-09-30 1 1949-12-31 1 1950-03-31 0 1950-06-30 0 1953-09-30 1 1953-12-31 1" ) df %>% mutate( changed = Recession != lag(Recession, default = Recession[1]), to_recession = str_c("Recession ", cumsum(changed & as.logical(Recession)) + 1), Recession_Num = if_else(Recession == 1, to_recession, "No Recession") ) %>% select(-changed, -to_recession) #> # A tibble: 7 x 3 #> Date Recession Recession_Num #> <date> <int> <chr> #> 1 1949-06-30 1 Recession 1 #> 2 1949-09-30 1 Recession 1 #> 3 1949-12-31 1 Recession 1 #> 4 1950-03-31 0 No Recession #> 5 1950-06-30 0 No Recession #> 6 1953-09-30 1 Recession 2 #> 7 1953-12-31 1 Recession 2

Created on 2018-10-30 by the reprex package (v0.2.1)

**How to get cumsum to work on consecutive values and restart if ,** I essentially just want to restart cumsum everytime there is a break in the sequence. Thank you for any help/advice/time you can offer! This uses the compare-cumsum-groupby pattern to find the contiguous groups, because df[2].diff().ne(0) gives us a True whenever a value isn't the same as the previous, and the cumulative sum of those gives us a new number whenever a new group of 1s starts.

Here is a `cumsum`

trick.

x <- c(1, 1, 1, 0, 0, 1, 1) i <- cumsum(c(1, diff(x) != 0) & as.logical(x)) ifelse(x == 0, "No Recession", paste("Recession", i)) #[1] "Recession 1" "Recession 1" "Recession 1" "No Recession" #[5] "No Recession" "Recession 2" "Recession 2"

**Cumulative sum - MATLAB cumsum,** B = cumsum(___, nanflag ) specifies whether to include or omit NaN values from the calculation for any of the previous An easy way to calculate a running total in Excel by using a Sum formula with a clever use of absolute and relative cell references; and 4 quick steps to make a cumulative graph in Excel 2016, 2013, 2010 and earlier.

Date <- as.Date(c('1949-06-30', '1949-09-30', '1949-12-31', '1950-03-31', '1950-06-30', '1953-09-30', '1953-12-31'), format = '%Y-%m-%d') Recession <- c(1,1,1,0,0,1,1) df <- data.frame(Date, Recession) find_seq_1s <- function(x) { count <- 0 in_seq <- FALSE output <- NULL for(i in x) { if(i == 1 && in_seq == FALSE) { count <- count + 1 in_seq <- TRUE output <- c(output, paste('Recession', as.character(count))) } else if(i == 1 && in_seq == TRUE) { output <- c(output, paste('Recession', as.character(count))) } else { in_seq <- FALSE output <- c(output, 'No Recession') } } return(output) } df$Rec_Seq <- find_seq_1s(df$Recession)

**How to do a running total in Excel (Cumulative Sum formula),** For this, enter deposits as positive numbers, and withdrawals as negative numbers in some column (column C in this example). And then, to show In statistical quality control, the CUSUM (or cumulative sum control chart) is a sequential analysis technique developed by E. S. Page of the University of Cambridge. It is typically used for monitoring change detection. CUSUM was announced in Biometrika, in 1954, a few years after the publication of Wald's SPRT algorithm.

unlist(lapply(1:nrow(df), FUN = function(x) ifelse(df$recession[x]==1, paste("Recession", x), "No Recession")))

**Finding the Sum of Consecutive Numbers,** Summing a sequence might be easier for a few values, but as the number of terms in the If you look at the cumulative sum as you go through this sequence:. 'includenan' — Include NaN values from the input when computing the cumulative sums, resulting in NaN values in the output. 'omitnan' — Ignore all NaN values in the input. The sum of elements containing NaN values is the sum of all non-NaN elements. If all elements are NaN, then cumsum returns 0.

**Sum of Sequence: Consecutive Integers and Multiples,** The list also contains a Null value and the skipna parameter is kept default, that is True. filter_none. edit close. play_arrow. link brightness_4 code Please see attachment. I'm trying to get a total sum of row 3 only when there is a "1" in each days of the week in the corresponding decimal point in row 2. Looking at the entire Row 3, cell J3 and R3 have a "1" in consecutive cells so it should be counted as one value (0.25).

**Python,** In OG PS Q172, a similar approach has been used for even numbers. Note that the formula n(n+1)/2 is used only for adding n consecutive integers starting from 1. I search for a possibility in Power Query or DAX, to calculate the difference between consequtive row values and store the result in a new column. The table is sorted e.g. along a timestamp and e.g. the time differences should be calculated each row to the previous. Or if we have a measured signal a

**np.cumsum - Numpy and Scipy,** @pnvinod . So if you have a lot of sales each day and a row for each sales it won't work. The solution will be to : Either have a Index column (like Sale1; Sale2 that identify each sale)