1

I'm working on time-series analyses and I'm hoping to develop multiple datasets with different units of analysis. Namely: the units in data set 1 will be districts in country X for 2-week periods within a span of 4 years (districtYearPeriodCode), the units in data set 2 will be districts in country X for 4-week periods within a span of 4 years, and so forth.

I have created a number of data frames containing start and end dates for each interval, as well as an interval ID. The one below is for the 2-week intervals.

begin <- seq(ymd('2004-01-01'),ymd('2004-06-30'), by = as.difftime(weeks(2)))
end <- seq(ymd('2004-01-14'),ymd('2004-06-30'), by = as.difftime(weeks(2)))
interval <- seq(1,13,1)
df2 <- data.frame(begin, end, interval)

        begin        end interval
1  2004-01-01 2004-01-14        1
2  2004-01-15 2004-01-28        2
3  2004-01-29 2004-02-11        3
4  2004-02-12 2004-02-25        4
5  2004-02-26 2004-03-10        5
6  2004-03-11 2004-03-24        6
7  2004-03-25 2004-04-07        7
8  2004-04-08 2004-04-21        8
9  2004-04-22 2004-05-05        9
10 2004-05-06 2004-05-19       10
11 2004-05-20 2004-06-02       11
12 2004-06-03 2004-06-16       12
13 2004-06-17 2004-06-30       13

In addition to this I have a data frame that contains observations for events, dates included. It looks something like this:

new.df3 <- data.frame(dates5, districts5)
new.df3

  dates5 districts5
1 2004-01-01         d1
2 2004-01-02         d2
3 2004-01-03         d3
4 2004-01-04         d4
5 2004-01-05         d5

Is there a function I can write or a command I can use to end up with something like this?

      dates5 districts5 interval5
1 2004-01-01         d1         1
2 2004-01-02         d2         1
3 2004-01-03         d3         1
4 2004-01-04         d4         1
5 2004-01-05         d5         1

I have been trying to find an answer in the lubridate package, or in other threads but all answers seem to be tailored at finding out whether a date falls within a specific time interval instead of identifying the interval a date falls into from a group of intervals.

Much appreiciated!

1 Answers1

0

I used the purrr approached outlined by @alistair in here. I reproduce it below:

elements %>% 
    map(~intervals$phase[.x >= intervals$start & .x <= intervals$end]) %>% 
    # Clean up a bit. Shorter, but less readable: map_chr(~.x[1] %||% NA)
    map_chr(~ifelse(length(.x) == 0, NA, .x))
## [1] "a" "a" "a" NA  "b" "b" "c"