rdplyrpanel-data

Identifying crisis episodes


New R user here. I have a relatively large panel of countries, and I want to identify recession episodes.

labor_pivoted_crisis=labor_pivoted%>%
  group_by(country)%>%
  mutate(crisis=ifelse(dplyr::lag(gdp_growth,1)<0 &dplyr::lag(gdp_growth,2)<0)&
           dplyr::lag(gdp_growth,3)<0 & dplyr::lag(gdp_growth,4)<0 &dplyr::lag(gdp_growth,5)<0,1,0 )

This is my attempt. The idea is that for each country, I want to create a variable called crisis if there are 6 consecutive months of negative gdp growth. The code I have fails pretty badly. I don't know if this is the correct of way of approaching this, it certainly seemed the most intuitive. Moreover, I'd like to have all periods marked with 1. That is, if for australia, from 1980:1 - 1980:7 there's negative gdp growth, have crisis=1 for all

Here's a sample of my data

structure(list(country = structure(c("AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS", 
"AUS", "AUS", "AUS", "AUS", "AUS", "AUS", "AUS"), label = "Country", format.stata = "%3s"), 
    time = structure(c("1978-02", "1978-03", "1978-04", "1978-05", 
    "1978-06", "1978-07", "1978-08", "1978-09", "1978-10", "1978-11", 
    "1978-12", "1979-01", "1979-02", "1979-03", "1979-04", "1979-05", 
    "1979-06", "1979-07", "1979-08", "1979-09", "1979-10", "1979-11", 
    "1979-12", "1980-01", "1980-02", "1980-03", "1980-04", "1980-05", 
    "1980-06", "1980-07", "1980-08", "1980-09", "1980-10", "1980-11", 
    "1980-12", "1981-01", "1981-02", "1981-03", "1981-04", "1981-05", 
    "1981-06", "1981-07", "1981-08", "1981-09", "1981-10", "1981-11", 
    "1981-12", "1982-01", "1982-02", "1982-03", "1982-04", "1982-05", 
    "1982-06", "1982-07", "1982-08", "1982-09", "1982-10", "1982-11", 
    "1982-12", "1983-01", "1983-02", "1983-03", "1983-04", "1983-05", 
    "1983-06", "1983-07", "1983-08", "1983-09", "1983-10", "1983-11", 
    "1983-12", "1984-01", "1984-02", "1984-03", "1984-04", "1984-05", 
    "1984-06", "1984-07", "1984-08", "1984-09", "1984-10", "1984-11", 
    "1984-12", "1985-01", "1985-02", "1985-03", "1985-04", "1985-05", 
    "1985-06", "1985-07", "1985-08", "1985-09", "1985-10", "1985-11", 
    "1985-12", "1986-01", "1986-02", "1986-03", "1986-04", "1986-05", 
    "1986-06", "1986-07", "1986-08", "1986-09", "1986-10", "1986-11", 
    "1986-12", "1987-01", "1987-02", "1987-03", "1987-04", "1987-05", 
    "1987-06", "1987-07", "1987-08", "1987-09", "1987-10", "1987-11", 
    "1987-12", "1988-01", "1988-02", "1988-03", "1988-04", "1988-05", 
    "1988-06", "1988-07", "1988-08", "1988-09", "1988-10", "1988-11", 
    "1988-12", "1989-01", "1989-02", "1989-03", "1989-04", "1989-05", 
    "1989-06", "1989-07", "1989-08", "1989-09", "1989-10", "1989-11", 
    "1989-12", "1990-01", "1990-02", "1990-03", "1990-04", "1990-05", 
    "1990-06", "1990-07", "1990-08", "1990-09", "1990-10", "1990-11", 
    "1990-12", "1991-01", "1991-02", "1991-03", "1991-04", "1991-05", 
    "1991-06", "1991-07", "1991-08", "1991-09", "1991-10", "1991-11", 
    "1991-12", "1992-01", "1992-02", "1992-03", "1992-04", "1992-05", 
    "1992-06", "1992-07", "1992-08", "1992-09", "1992-10", "1992-11", 
    "1992-12", "1993-01", "1993-02", "1993-03", "1993-04", "1993-05", 
    "1993-06", "1993-07", "1993-08", "1993-09", "1993-10", "1993-11", 
    "1993-12", "1994-01", "1994-02", "1994-03", "1994-04", "1994-05", 
    "1994-06", "1994-07", "1994-08", "1994-09"), label = "TIME", format.stata = "%7s"), 
    hourly_earnings = structure(c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), label = "1 Value", format.stata = "%9.0g"), 
    indpro = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "2 Value", format.stata = "%9.0g"), 
    unrate = structure(c(6.6452319, 6.3034739, 6.268134, 6.2101133, 
    6.3039926, 6.2228361, 6.4906549, 6.2525847, 6.1638032, 6.3319558, 
    6.406259, 6.3721294, 6.2576052, 6.3037519, 6.4990554, 6.2043217, 
    6.251819, 6.2734121, 6.1185826, 6.2339749, 6.3843962, 6.0456562, 
    6.1209598, 6.1473602, 5.9991263, 5.9372182, 6.1150846, 6.2465376, 
    6.3234529, 6.0763653, 6.1775342, 6.1422493, 6.0556962, 5.8509441, 
    6.0518374, 5.9165447, 5.6169429, 5.7466508, 5.5554355, 5.5893384, 
    5.3745258, 5.8267606, 5.8465551, 5.8130612, 5.8729426, 5.9957551, 
    6.0276806, 5.9822529, 6.3088397, 6.3235955, 6.3336512, 6.593329, 
    6.7673598, 6.8822885, 7.0119384, 7.4303138, 8.2955154, 8.6800725, 
    9.4027245, 9.2988598, 9.659196, 9.9522011, 10.1528479, 10.3236766, 
    10.2045346, 10.514744, 10.2195119, 10.3386567, 10.0084493, 
    9.6548088, 9.4579474, 9.4965197, 9.3861598, 9.2091037, 9.3709383, 
    8.9370226, 9.0969641, 8.9134629, 8.8289107, 8.7352325, 8.6957841, 
    8.6795379, 8.5199742, 8.5520436, 8.3897283, 8.6664481, 8.4020604, 
    8.3761971, 8.5723753, 8.3032604, 8.1557906, 8.0576504, 7.8488685, 
    7.9505862, 7.7848784, 7.8062466, 8.0030188, 7.9282314, 7.9012122, 
    7.8467509, 7.6544219, 8.1104127, 8.2393568, 8.3383176, 8.3099757, 
    8.3190215, 8.3634037, 8.1869884, 8.2108997, 8.4366959, 8.30065, 
    8.2019266, 8.0095199, 8.0773936, 8.1052635, 7.7553179, 8.0858223, 
    7.9311209, 7.7280755, 7.6310194, 7.4167097, 7.5053738, 7.9133209, 
    7.4512939, 7.5758213, 6.7586651, 7.068309, 6.9343654, 6.8177348, 
    6.6126209, 6.8324758, 6.8064323, 6.637505, 6.3413413, 6.2005133, 
    6.2797032, 6.012372, 6.0450667, 5.9438161, 6.0309437, 5.9239818, 
    5.7865576, 5.8480236, 6.1113595, 6.3914308, 6.1859221, 6.2796072, 
    6.5043555, 6.609129, 7.0925505, 7.2113541, 7.3742217, 7.6371317, 
    7.9458501, 8.0191965, 8.3987797, 8.6166366, 9.1835238, 9.8616106, 
    9.5026683, 9.5461319, 9.6821056, 9.7907975, 10.0327569, 9.98741, 
    10.197996, 10.473928, 10.3485338, 10.4050059, 10.4970614, 
    10.4716481, 10.6192189, 10.8115274, 11.1327115, 10.7222753, 
    10.6168255, 11.0914899, 11.1365366, 11.2177857, 10.8340971, 
    11.0144785, 10.8582907, 10.7660905, 10.7867614, 10.9932898, 
    10.9414599, 10.982996, 10.7418171, 10.957649, 10.9492515, 
    10.7568711, 10.5682322, 10.3991028, 10.3536576, 10.0628966, 
    9.7670342, 9.8199101, 9.6303637, 9.4445722, 9.3534724), label = "3 Value", format.stata = "%9.0g"), 
    retail_trade = structure(c(NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), label = "4 Value", format.stata = "%9.0g"), 
    tot_manuf = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
    NA_real_, NA_real_, NA_real_, NA_real_), label = "5 Value", format.stata = "%9.0g"), 
    year = structure(c("1978", "1978", "1978", "1978", "1978", 
    "1978", "1978", "1978", "1978", "1978", "1978", "1979", "1979", 
    "1979", "1979", "1979", "1979", "1979", "1979", "1979", "1979", 
    "1979", "1979", "1980", "1980", "1980", "1980", "1980", "1980", 
    "1980", "1980", "1980", "1980", "1980", "1980", "1981", "1981", 
    "1981", "1981", "1981", "1981", "1981", "1981", "1981", "1981", 
    "1981", "1981", "1982", "1982", "1982", "1982", "1982", "1982", 
    "1982", "1982", "1982", "1982", "1982", "1982", "1983", "1983", 
    "1983", "1983", "1983", "1983", "1983", "1983", "1983", "1983", 
    "1983", "1983", "1984", "1984", "1984", "1984", "1984", "1984", 
    "1984", "1984", "1984", "1984", "1984", "1984", "1985", "1985", 
    "1985", "1985", "1985", "1985", "1985", "1985", "1985", "1985", 
    "1985", "1985", "1986", "1986", "1986", "1986", "1986", "1986", 
    "1986", "1986", "1986", "1986", "1986", "1986", "1987", "1987", 
    "1987", "1987", "1987", "1987", "1987", "1987", "1987", "1987", 
    "1987", "1987", "1988", "1988", "1988", "1988", "1988", "1988", 
    "1988", "1988", "1988", "1988", "1988", "1988", "1989", "1989", 
    "1989", "1989", "1989", "1989", "1989", "1989", "1989", "1989", 
    "1989", "1989", "1990", "1990", "1990", "1990", "1990", "1990", 
    "1990", "1990", "1990", "1990", "1990", "1990", "1991", "1991", 
    "1991", "1991", "1991", "1991", "1991", "1991", "1991", "1991", 
    "1991", "1991", "1992", "1992", "1992", "1992", "1992", "1992", 
    "1992", "1992", "1992", "1992", "1992", "1992", "1993", "1993", 
    "1993", "1993", "1993", "1993", "1993", "1993", "1993", "1993", 
    "1993", "1993", "1994", "1994", "1994", "1994", "1994", "1994", 
    "1994", "1994", "1994"), label = "year", format.stata = "%4s"), 
    gdp_value = structure(c(98.4695302344303, 98.5169722193382, 
    98.612532792272, 98.7531336904206, 98.9345864968255, 99.1452728517372, 
    99.3733762609892, 99.611902663658, 99.8623487236206, 100.12151982687, 
    100.3672509773, 100.543287810952, 100.591198051117, 100.487338035044, 
    100.288154771531, 100.082274092272, 99.9495273724208, 99.9212404519395, 
    99.9966795134124, 100.151475149294, 100.338211123034, 100.505201256861, 
    100.613066446539, 100.652794829522, 100.633147954404, 100.572772467456, 
    100.496519788948, 100.43022885188, 100.396762303709, 100.411438007535, 
    100.481775429173, 100.603767075081, 100.754074224018, 100.906952300005, 
    101.049124566129, 101.194475808986, 101.368093834761, 101.590029977222, 
    101.856925359683, 102.147928384445, 102.427262067845, 102.64504104331, 
    102.751453041033, 102.715512411677, 102.551523363691, 102.296250984933, 
    101.995004716392, 101.694416272287, 101.429242989447, 101.20947213835, 
    101.005026001166, 100.76808414494, 100.454002675226, 100.044282750873, 
    99.5378103034852, 98.9487763818231, 98.311247009414, 97.6674368592102, 
    97.059759516916, 96.5236800244664, 96.0929482502808, 95.7996576036827, 
    95.6667745869191, 95.7088343048915, 95.9207056123794, 96.2617956549094, 
    96.6782006115433, 97.1238570436254, 97.5762536041193, 98.0252810406405, 
    98.4610089381248, 98.8610594587839, 99.1987814318825, 99.4536797242027, 
    99.6259402942475, 99.7254904224033, 99.7664602164719, 99.7664900390493, 
    99.7444949408169, 99.721128878131, 99.7206153238704, 99.7669153700244, 
    99.8781066751096, 100.057190217528, 100.296670013331, 100.577882989028, 
    100.865623403148, 101.11825701868, 101.297223870673, 101.378921731947, 
    101.351664241806, 101.219269909748, 101.012025109855, 100.76603972427, 
    100.508110596336, 100.243660345686, 99.9685247392773, 99.6810299488812, 
    99.3946297425669, 99.1312618965711, 98.9148145466343, 98.7631535412899, 
    98.688045689128, 98.6904351795826, 98.7501600586484, 98.8422125393387, 
    98.948704520634, 99.0684586329116, 99.210239267737, 99.3850006987213, 
    99.596502710532, 99.8441034744245, 100.122827771273, 100.420749594301, 
    100.720079307938, 100.9968183465, 101.220303011515, 101.360159336626, 
    101.39716053808, 101.340006954349, 101.211635853681, 101.042292331188, 
    100.867912146391, 100.723090733347, 100.63483443058, 100.614555815063, 
    100.664545405395, 100.779650663219, 100.944018706483, 101.141283816305, 
    101.361375288876, 101.605164547861, 101.875234412478, 102.165043986913, 
    102.446246343854, 102.681488120928, 102.838122921775, 102.905475770223, 
    102.886952437687, 102.798580276709, 102.67378936632, 102.546659576147, 
    102.438210183775, 102.343282026901, 102.243328285064, 102.118337359094, 
    101.959240092111, 101.764883372088, 101.541635432717, 101.305669208733, 
    101.071245564817, 100.840222051863, 100.591737024316, 100.299762323155, 
    99.9517790519871, 99.5702598283114, 99.1926486145746, 98.8541067850158, 
    98.5704135321868, 98.3452503903445, 98.1733859398282, 98.0395332550919, 
    97.9279513246968, 97.8291396327947, 97.7447910049851, 97.6824239813926, 
    97.648873962816, 97.6420701409995, 97.6577840561963, 97.6941970999024, 
    97.7546065018755, 97.8465530745318, 97.9789793570575, 98.1568498169539, 
    98.3793860125881, 98.6346649132098, 98.890572457773, 99.1089590705996, 
    99.2595236859213, 99.3361029377613, 99.3452816030623, 99.2998368411183, 
    99.2166651200242, 99.1158674903585, 99.0222909984354, 98.9681075759982, 
    98.9840383202244, 99.0876268724422, 99.2661563846128, 99.493232553004, 
    99.7394949309265, 99.9795362399226, 100.193106620391, 100.366456200796, 
    100.495236018581, 100.579856488046, 100.623698364593, 100.634182465959, 
    100.616937320912, 100.572142389189), label = "gdp_value", format.stata = "%9.0g"), 
    gdp_growth = structure(c(-0.00568693496756367, 0.0481677528503255, 
    0.0969520827574399, 0.142477589379286, 0.183575244017042, 
    0.212728780257709, 0.229805623458734, 0.239742877669435, 
    0.251106285466385, 0.259192154068977, 0.245132206151855, 
    0.175239068853816, 0.0476400065019256, -0.103302945536399, 
    -0.1984139857651, -0.205500135803316, -0.132725634990472, 
    -0.0283052103898562, 0.0754700379702378, 0.154681083127084, 
    0.186279934105471, 0.166288921631352, 0.107265442619564, 
    0.0394785119016916, -0.0195213584303744, -0.0600136304472976, 
    -0.0758471682154038, -0.0659851810888235, -0.0333287356328427, 
    0.0146166379952462, 0.0700246895442014, 0.121333099689558, 
    0.149293591831423, 0.15161889028219, 0.140795257876025, 0.143738805690852, 
    0.171421668470817, 0.218701500342267, 0.262373585417119, 
    0.285290488726719, 0.273086737819828, 0.212392462588795, 
    0.103616186554678, -0.0349843388628202, -0.159781215970956, 
    -0.249231422413843, -0.294918630611551, -0.295144107974021, 
    -0.261095565574454, -0.216909130248055, -0.202207268743049, 
    -0.23485980363489, -0.312174204874527, -0.408702242818482, 
    -0.507534044588009, -0.593526908469322, -0.646387018715711, 
    -0.657022951158215, -0.624133994881237, -0.553849920293725, 
    -0.447243310222056, -0.305682309823929, -0.138805571899692, 
    0.0439551507041003, 0.221126026719176, 0.354965096640836, 
    0.431642594810189, 0.459909686208881, 0.464711985156097, 
    0.459125462500776, 0.443520626095673, 0.405480311478534, 
    0.341030568230138, 0.256627509901985, 0.173057004047283, 
    0.0998740125143449, 0.041074133079988, 2.98923835906351e-05, 
    -0.0220490098064019, -0.0234286613857826, -0.00051499174622549, 
    0.0464189885195587, 0.111389019814112, 0.179141545384098, 
    0.239056946120986, 0.27998883403102, 0.285678714560067, 0.250152385175362, 
    0.176831236268438, 0.0806191231395914, -0.0268903581315705, 
    -0.130714064644533, -0.204958260710519, -0.243817887386921, 
    -0.25629646798988, -0.263460095394574, -0.274844190415546, 
    -0.287999629712221, -0.287730207045112, -0.265323579028376, 
    -0.218582907577325, -0.153442527326675, -0.0760773862880093, 
    0.00242122691807012, 0.0604990891720725, 0.0931741302961697, 
    0.107681377050284, 0.120953279978586, 0.143011488869949, 
    0.1759976484073, 0.212584675297922, 0.248295365557549, 0.278770570330256, 
    0.297114518555386, 0.297632197538533, 0.274383767946507, 
    0.221034454948832, 0.138074862411841, 0.0364980183084285, 
    -0.0563819510275287, -0.126753963656512, -0.167456388694198, 
    -0.172730475514715, -0.143678473631059, -0.0876611222000001, 
    -0.0201527225110354, 0.0496719143286306, 0.114280056311067, 
    0.162963602787691, 0.195229608501979, 0.217371525100241, 
    0.240226173654623, 0.265450652330124, 0.284071136931097, 
    0.274865119640921, 0.229361359462299, 0.152428111730885, 
    0.0654726088997748, -0.0180019573580381, -0.0859293944979633, 
    -0.121467354417693, -0.123895846725208, -0.105812104686098, 
    -0.0927116631965319, -0.0977128949322115, -0.122323275416036, 
    -0.155918451405768, -0.190803892718172, -0.219617187302834, 
    -0.232654144295896, -0.231670444256515, -0.228836545537625, 
    -0.246718698195547, -0.290679205137678, -0.347546509802843, 
    -0.382433630701229, -0.379961906369797, -0.34188104583972, 
    -0.287394339505642, -0.228690024438905, -0.17490910535658, 
    -0.136436179686061, -0.113878016746316, -0.100953380345814, 
    -0.0862575423671963, -0.0638263465138067, -0.0343519131440928, 
    -0.00696788256160374, 0.016092091103026, 0.0372794213934213, 
    0.0618160897212228, 0.0940143500455903, 0.135249271193505, 
    0.181374825198422, 0.226458281139053, 0.259148058007419, 
    0.259113914909825, 0.220593149588844, 0.151802990216066, 
    0.0771207875394531, 0.00923958258969293, -0.0457547238942979, 
    -0.0837932616572168, -0.101645087689306, -0.0944558075982371, 
    -0.0547333852348508, 0.0160955509610261, 0.104597052346023, 
    0.180011248031686, 0.228493625786186, 0.247210893961292, 
    0.240379119779366, 0.213386263432813, 0.172865976704806, 
    0.128227372783041, 0.0841680323562244, 0.0435796248957843, 
    0.0104185747481722, -0.0171379370286218, -0.0445301827790345
    ), label = "gdp_growth", format.stata = "%9.0g"), banking_crisis = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0), systemic_crisis = c(0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0), currency_crisis = c(1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 
    2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0), fin_crisis = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0)), row.names = c(NA, -200L), class = c("tbl_df", "tbl", 
"data.frame"), label = "Written by R.")

Solution

  • EDIT- this approach now separates out each streak into grp, and marks as crisis any grp that is declines and length >= 6.

    library(dplyr)
    labor_pivoted |>
      mutate(decline = gdp_growth < 0,
             switch = decline != lag(decline, 1, FALSE),
             grp = cumsum(switch), .by = country) |>
      mutate(crisis = decline & n() >= 6, .by = c(country, grp)) 
    

    Result

    # A tibble: 200 × 18
       country time    hourly_earnings indpro unrate retail_trade tot_manuf year  gdp_value gdp_growth banking_crisis systemic_crisis currency_crisis fin_crisis decline switch   grp crisis
       <chr>   <chr>             <dbl>  <dbl>  <dbl>        <dbl>     <dbl> <chr>     <dbl>      <dbl>          <dbl>           <dbl>           <dbl>      <dbl> <lgl>   <lgl>  <int> <lgl> 
     1 AUS     1978-02              NA     NA   6.65           NA        NA 1978       98.5   -0.00569              0               0               1          1 TRUE    TRUE       1 FALSE 
     2 AUS     1978-03              NA     NA   6.30           NA        NA 1978       98.5    0.0482               0               0               1          1 FALSE   TRUE       2 FALSE 
     3 AUS     1978-04              NA     NA   6.27           NA        NA 1978       98.6    0.0970               0               0               1          1 FALSE   FALSE      2 FALSE 
     4 AUS     1978-05              NA     NA   6.21           NA        NA 1978       98.8    0.142                0               0               1          1 FALSE   FALSE      2 FALSE 
     5 AUS     1978-06              NA     NA   6.30           NA        NA 1978       98.9    0.184                0               0               1          1 FALSE   FALSE      2 FALSE 
     6 AUS     1978-07              NA     NA   6.22           NA        NA 1978       99.1    0.213                0               0               1          1 FALSE   FALSE      2 FALSE 
     7 AUS     1978-08              NA     NA   6.49           NA        NA 1978       99.4    0.230                0               0               1          1 FALSE   FALSE      2 FALSE 
     8 AUS     1978-09              NA     NA   6.25           NA        NA 1978       99.6    0.240                0               0               1          1 FALSE   FALSE      2 FALSE 
     9 AUS     1978-10              NA     NA   6.16           NA        NA 1978       99.9    0.251                0               0               1          1 FALSE   FALSE      2 FALSE 
    10 AUS     1978-11              NA     NA   6.33           NA        NA 1978      100.     0.259                0               0               1          1 FALSE   FALSE      2 FALSE 
    # ℹ 190 more rows
    # ℹ Use `print(n = ...)` to see more rows