1

I'm trying to plot the Recession Shading periods in R. Consider the following example, recession periods are recognized as 1, and non recession periods are 0.

Date           Recession  
1918-09-01     1  
1918-10-01     1  
1918-11-01     1  
1918-12-01     1  
1919-01-01     1  
1919-02-01     1  
1919-03-01     1  
1919-04-01     0  
1919-05-01     0  
1919-06-01     0  
1919-07-01     0  
1919-08-01     0  
1919-09-01     0  
1919-10-01     0  
1919-11-01     0  
1919-12-01     0  
1920-01-01     0  
1920-02-01     1  
1920-03-01     1  
1920-04-01     1  
1920-05-01     1  

Can anyone help me to pick up the starting date and ending dates of the recession periods? For example:

Start                 End
1918-09-01     1919-03-01
1920-02-01     1920-05-01

The same question has been asked few years ago, but I think the answer is not able solve this question. see R Recession Dates Conversion

Thanks in advance!

James
  • 57
  • 5
  • Why is python tagged? And what did you try? SO is not a code-writing service. Otherwise this is too broad. – Parfait Jul 25 '17 at 14:56
  • Just think maybe python could be used to do this. I tried to used 'diff(...)' in the recession column, but failed to do that. Do you have any idea how to do that? – James Jul 25 '17 at 16:53
  • Possible duplicate of [How to detect start and end of constant periods?](https://stackoverflow.com/questions/45404638/how-to-detect-start-and-end-of-constant-periods) – tuomastik Jul 31 '17 at 04:10

1 Answers1

1

Using the rleid() function from the data.table package:

library(data.table)
data.table(DF)[, .(min(Date), max(Date)), by = .(rleid(Recession), Recession)][
  Recession == 1, .(Start = V1, End = V2)]
        Start        End
1: 1918-09-01 1919-03-01
2: 1920-02-01 1920-05-01

Explanation

The first data.table expression finds start and end dates of all periods. rleid() is a convenience function for generating a run-length type id column to be used in grouping operations.

data.table(DF)[, .(min(Date), max(Date)), by = .(rleid(Recession), Recession)]
   rleid Recession         V1         V2
1:     1         1 1918-09-01 1919-03-01
2:     2         0 1919-04-01 1920-01-01
3:     3         1 1920-02-01 1920-05-01

The second expression picks only the Recession periods and returns the Start and End dates.

Data

DF <- readr::read_table(
  "Date           Recession  
  1918-09-01     1  
  1918-10-01     1  
  1918-11-01     1  
  1918-12-01     1  
  1919-01-01     1  
  1919-02-01     1  
  1919-03-01     1  
  1919-04-01     0  
  1919-05-01     0  
  1919-06-01     0  
  1919-07-01     0  
  1919-08-01     0  
  1919-09-01     0  
  1919-10-01     0  
  1919-11-01     0  
  1919-12-01     0  
  1920-01-01     0  
  1920-02-01     1  
  1920-03-01     1  
  1920-04-01     1  
  1920-05-01     1  "
)
Community
  • 1
  • 1
Uwe
  • 41,420
  • 11
  • 90
  • 134