I have a simple binary time series, which lists if the USA is in a recession or not (https://fred.stlouisfed.org/series/USRECM) and I want to add it as a shaded region in a dygraph. However dygraphs only takes in a list (see link and below) so I need a quick an easy way to create a list of each recession period, which is marked by a 1.
Any efficient ways of doing this in R without me writing function that looks at current and lagged values.
recession_periods <- list(
list(from = "1920-1-1", to = "1930-1-1"),
list(from = "1940-1-1", to = "1950-1-1"),
list(from = "1960-1-1", to = "1970-1-1")
)
Yes, with the 'seq' function for dates and the
lubridate
package. For instance, this will create a sequence of dates separated by 10 years.This will create something like this:
You can create many other pairs of dates, by different number of years, weeks, days, etc. using the
by
parameter inseq
and thelubridate
functions.