简体   繁体   English

查找发生日期的时间间隔

[英]Finding the time interval in which a date occurs

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. 即:数据集1中的单位将是X国在4年的跨度内的两周期间(districtYearPeriodCode),数据集2中的单位将是X国在4年的跨度内的四周期间的区域4年,依此类推。

I have created a number of data frames containing start and end dates for each interval, as well as an interval ID. 我创建了许多数据框,其中包含每个间隔的开始和结束日期,以及间隔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. 我一直在尝试在lubridate包或其他线程中找到答案,但是所有答案似乎都是为了确定日期是否落在特定时间间隔内,而不是从一组间隔中确定日期落入的间隔而设计的。

Much appreiciated! 非常感激!

I used the purrr approached outlined by @alistair in here . 我在这里使用@alistair概述的方法。 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM