简体   繁体   English

如何在 R 的日期范围及其各自的值之间找到所有月份的第三个星期日日期

[英]How to find third Sunday date for all months between a date range in R and their respective values

For a particular date range, for example between 2020-01-29 and 2021-05-02, I want to find out dates for every 3rd Sunday of every month along with their associated value in a data.frame .对于特定的日期范围,例如 2020-01-29 和 2021-05-02 之间,我想在data.frame中找出每个月的第三个星期日的日期及其相关value

Additionally, if there is any 5th Monday in any month then I want to obtain its date and corresponding value in a separate data.frame .此外,如果任何一个月有第 5 个星期一,那么我想在单独的data.frame中获取其日期和相应的值。

Please note that it needs to be between a date range from those given in the data.frame .请注意,它需要介于data.frame中给出的日期范围之间。

## for creating data frame in R wrt dates and values
dates_seq<-(seq(as.Date("2019/12/28"), by = "day", length.out = 1000))
dates_seq<-as.data.frame(dates_seq)
values<-seq(1:1000)
df<-as.data.frame(cbind(dates_seq,values))

To summarize, I want to find the third Sunday date for every month and it's corresponding value and the fifth Monday for every month if there is any along with it's value.总而言之,我想找到每个月的第三个星期日日期及其对应的值,以及每个月的第五个星期一(如果有的话)以及它的值。

Here's a base R approach:这是一个基本的 R 方法:

# Get date between 2020-01-29 and 2021-05-0
temp <- subset(df, dates_seq >= as.Date('2020-01-29') & 
                    dates_seq <= as.Date('2021-05-02'))
#Add weekday
temp$week_day <- weekdays(temp$dates_seq)
#Add week number for each month
temp$week_number <- ave(temp$week_day, temp$week_day, 
                        format(temp$dates_seq, "%Y-%m"), FUN = seq_along)
#Subset 3rd Sunday and 5th Monday
subset(temp, week_number == 3 & week_day == 'Sunday' | 
             week_number == 5 & week_day == 'Monday')


#     dates_seq values week_day week_number
#51  2020-02-16     51   Sunday           3
#79  2020-03-15     79   Sunday           3
#94  2020-03-30     94   Monday           5
#114 2020-04-19    114   Sunday           3
#142 2020-05-17    142   Sunday           3
#177 2020-06-21    177   Sunday           3
#185 2020-06-29    185   Monday           5
#205 2020-07-19    205   Sunday           3
#233 2020-08-16    233   Sunday           3
#248 2020-08-31    248   Monday           5
#268 2020-09-20    268   Sunday           3
#296 2020-10-18    296   Sunday           3
#324 2020-11-15    324   Sunday           3
#339 2020-11-30    339   Monday           5
#359 2020-12-20    359   Sunday           3
#387 2021-01-17    387   Sunday           3
#422 2021-02-21    422   Sunday           3
#450 2021-03-21    450   Sunday           3
#458 2021-03-29    458   Monday           5
#478 2021-04-18    478   Sunday           3

As in lubridate Sundays are the 1st day of the week, this code will give you a data frame containing all third Sundays:由于lubridate星期日是一周的第一天,此代码将为您提供一个包含所有第三个星期日的数据框:

df <- df %>%
  mutate(dates_seq = as.Date(dates_seq)) %>%
  mutate(year = year(dates_seq),
         month = month(dates_seq),
         day = wday(dates_seq)) %>%
  filter(day == 1) %>%
  group_by(year, month) %>%
  slice(3)

You could do a match with the original data frame to find the row index.您可以与原始数据框进行匹配以查找行索引。

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

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