简体   繁体   English

获取每月的第二个和第四个星期六

[英]Get 2nd and 4th Saturday of Month

d = seq(as.Date("2018-01-01"),Sys.Date(),by='day')
saturdayList = d[weekdays(d)=='Saturday']

How to get the 2nd and 4th saturday of a month from above list of Saturday dates? 如何从上面的星期六日期列表中获取一个月的第二个和第四个星期六?

You can split the vector into a list and extract the 2nd and 4th element per month: 您可以将向量分成一个列表,并每月提取第二个和第四个元素:

lapply(split(saturdayList, format(saturdayList, "%Y-%m")), `[`, c(2, 4))
# $`2018-01`
# [1] "2018-01-13" "2018-01-27"
# 
# $`2018-02`
# [1] "2018-02-10" "2018-02-24"
# 
# $`2018-03`
# [1] "2018-03-10" "2018-03-24"
# 
# $`2018-04`
# [1] "2018-04-14" "2018-04-28"
# ...

Without NA's: 没有NA:

lapply(split(saturdayList, format(saturdayList, "%Y-%m")), function(x)
       na.omit(x[c(2,4)]))

Assumptions: 假设:

  • The input is ordered by date (as in the example) 输入按日期排序(如示例中所示)
  • The input is a complete sequence without missing saturdays (as in the example) 输入是一个完整的序列,没有丢失星期六(如示例中所示)

data.table solution data.table解决方案

The solution is slower (on the sample data), than the 'lapply-solution' from docendo discimus. 解决方案(在样本数据上)比docendo discimus提出的“ lapply-solution”要慢。 However, it is very flexible, and will alsoperform quite well on larger data-sets. 但是,它非常灵活,并且在较大的数据集上也能很好地执行。

library(data.table)
#build a data.table out of the vector
DT <- data.table(date = d)
#group by month, select the second and 4 row where wday == 7 (i.e. Saturday)
DT[ DT[ , .I[wday(date) == 7][c(2,4)], by = .(month(date)) ]$V1 ]

output 产量

# date
# 1: 2018-01-13
# 2: 2018-01-27
# 3: 2018-02-10
# 4: 2018-02-24
# 5: 2018-03-10
# 6: 2018-03-24
# 7: 2018-04-14
# 8: 2018-04-28
# 9: 2018-05-12
# 10: 2018-05-26
# 11: 2018-06-09
# 12: 2018-06-23
# 13: 2018-07-14
# 14: 2018-07-28
# 15: 2018-08-11
# 16: 2018-08-25
# 17: 2018-09-08
# 18: 2018-09-22
# 19: 2018-10-13
# 20: 2018-10-27
# 21: 2018-11-10
# 22: 2018-11-24
# 23: 2018-12-08
# 24: 2018-12-22
# date

1) This calculates a vector of 2nd and 4th Saturdays directly from d although we could replace d with saturdayList and it would still work. 1)尽管我们可以将d替换为saturdayList ,但这直接从d计算了第二个和第四个星期六的向量,它仍然可以工作。

Define nextsat , based on nextfri in the zoo quickref vignette , which gives the next Saturday on or after the input date for each element of the input vector. 根据zoo quickref小插图中的nextfri定义nextsat ,它为输入向量的每个元素提供输入日期或之后的下一个星期六。 Then for each element of d find the first of the month, apply nextsat to give the first Saturday of the month and add 7 to get the second Saturday of each month. 然后对于d每个元素,找到该月的第一天,应用nextsat给出该月的第一个星期六,并添加7以得到每月的第二个星期六。 Add 14 to get the 4th Saturday and sort into a single vector. 加14得到第四个星期六,并分类为一个向量。 No packages are used. 不使用任何软件包。

nextsat <- function(x) 7 * ceiling(as.numeric(x-6+4) / 7) + 
  as.Date(6-4, origin = "1970-01-01")

sat2 <- unique(nextsat(as.Date(cut(d, "month")))) + 7
sort(c(sat2, sat2 + 14))

giving: 赠送:

 [1] "2018-01-13" "2018-01-20" "2018-02-10" "2018-02-17" "2018-03-10"
 [6] "2018-03-17" "2018-04-14" "2018-04-21" "2018-05-12" "2018-05-19"
[11] "2018-06-09" "2018-06-16" "2018-07-14" "2018-07-21" "2018-08-11"
[16] "2018-08-18" "2018-09-08" "2018-09-15" "2018-10-13" "2018-10-20"
[21] "2018-11-10" "2018-11-17" "2018-12-08" "2018-12-15" "2019-01-12"
[26] "2019-01-19" "2019-02-09" "2019-02-16" "2019-03-09" "2019-03-16"

2) This computes a vector satno giving the number of each Saturday within its month and then picks off the second and fourth. 2)计算一个向量satno给出一个月内每个星期六的数量,然后选择第二个和第四个。 This also does not use any packages. 这也不使用任何包。

satno <- ave(as.numeric(saturdayList), cut(saturdayList, "month"), FUN = seq_along)
saturdayList[satno %in% c(2, 4)]

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

相关问题 计算模式或第 2/3/4 个最常用值 - Calculating the mode or 2nd/3rd/4th most common value R:确定两个不同数据帧的两个文本字符串之间的第一,第二,第三,第四匹配 - R: Identify 1st, 2nd, 3rd, 4th match between two text strings of two different dataframes 在 R 中获取每个 11 月的第 4 个星期三 - Get the 4th Wednesday of each November in R 删除 r 矩阵中的每 2 和 5 列 - Removing every 2nd and 5th column in a matrix in r 序数日期的格式(月份中的日期后缀 -st、-nd、-rd、-th) - Format for ordinal dates (day of month with suffixes -st, -nd, -rd, -th) 获取第1个第1次和第2次出现之间的向量索引 - Get index of vector between 1nd and 2nd appearance of number 1 R lpsolve 获得第 2、3、Nth 最佳解决方案 - R lpsolve get 2nd,3rd,Nth best solution 确定一周中的某一天是否是 R 中的第 2/3 日等星期一/星期二/等 - Identify if a day of the week is 2nd/3rd etc Mon/Tues/etc day of the month in R 如何从第 4 列 grouped_by 第 1 列中的 id 的第 3 列中获取最大值(范围 1-2)? - How to get the largest value (range 1-2) from column 3 in a 4th column grouped_by the id's in column 1? 如何从 1 个 df 的第 i+1 列中获取值并计算到第 2 个 df 的第 i+1 列中每一行中的值的距离 - How to take value from i+1th column of 1 df and calculate distance to values in every row in i+1th column of 2nd df
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM