I'd like to know if it is possible to achieve the following using dplyr, or some tidyverse package...
Context: I am having trouble getting my data into a structure that will allow the use of geom_rect
. See this SO question for the motivation.
library(tis)
# Prepare NBER recession start end dates.
recessions <- data.frame(start = as.Date(as.character(nberDates()[,"Start"]),"%Y%m%d"),
end= as.Date(as.character(nberDates()[,"End"]),"%Y%m%d"))
dt <- tibble(date=c(as.Date('1983-01-01'),as.Date('1990-10-15'), as.Date('1993-01-01')))
Desired output:
date start end
1983-01-01 NA NA
1990-10-15 1990-08-01 1991-03-31
1993-01-01 NA NA
Appreciate any suggestions.
Note: Previous questions indicate that sqldf
is one approach to take. However, the data here involves dates and my understanding date is not a data type in SQLite.
In the spirit of 'write the code you wish you had':
df <- dt %>%
left_join(x=., y=recessions, date >= start & date <= end)