R找到上个月的上个月 [英] R find last weekday of month

查看:106
本文介绍了R找到上个月的上个月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用R找到本月的最后一个工作日(例如星期三)?在下面的代码中,我计算月份,月份,月份和工作日。 2014年1月有5个星期三,但2014年2月只有4个星期三,所以我不能使用最大(本月的一周)作为过滤器。任何帮助不胜感激,尽管我更喜欢使用基本的R函数。

How do I find the last weekday (e.g., Wednesday) of the month using R? In the code below, I calculate the month, day of the month, week of the month, and weekday. There are 5 Wednesdays in January 2014, but only 4 Wednesdays in February 2014, so I cannot use max(week of the month) as a filter. Any help is appreciated although I prefer to use the base R functions.

DF <- data.frame(DATE = seq(as.Date("2014-01-01"), as.Date("2014-12-31"), "day"))

DF$MONTH         <- as.numeric(format(DF$DATE, "%m"))
DF$DAY_OF_MONTH  <- as.numeric(format(DF$DATE, "%d"))
DF$WEEK_OF_MONTH <- ceiling(as.numeric(format(DF$DATE, "%d")) / 7)
DF$WEEKDAY       <- format(DF$DATE, "%A")

DF


推荐答案

我认为这是你以后的事: >

I think this is what you're after:

DF$last_weekday_o_month <- ave( 
  weekdays(DF$DATE), 
  months(DF$DATE), 
  FUN = function(x) tail(x[ !(x %in% c("Saturday","Sunday")) ], 1) 
)

查找最后一个工作日的特定日期....

To find the particular date that is the last weekday....

DF$last_weekdaydate_o_month <- ave( 
  DF$DATE, 
  months(DF$DATE), 
  FUN = function(x) tail(x[ !(weekdays(x) %in% c("Saturday","Sunday")) ], 1) 
)

结果看起来像...

          DATE last_weekday_o_month last_weekdaydate_o_month
1   2014-01-01               Friday               2014-01-31
2   2014-01-02               Friday               2014-01-31
3   2014-01-03               Friday               2014-01-31
4   2014-01-04               Friday               2014-01-31
5   2014-01-05               Friday               2014-01-31
6   2014-01-06               Friday               2014-01-31
...
360 2014-12-26            Wednesday               2014-12-31
361 2014-12-27            Wednesday               2014-12-31
362 2014-12-28            Wednesday               2014-12-31
363 2014-12-29            Wednesday               2014-12-31
364 2014-12-30            Wednesday               2014-12-31
365 2014-12-31            Wednesday               2014-12-31

如果你这样做了,当然你可以将 last_weekday_o_month 计算为工作日(last_weekdaydate_o_month)

If you did this first, of course you could compute last_weekday_o_month as weekdays(last_weekdaydate_o_month).

有几个包,这可以做更多优雅/可读,如@RichardScriven所建议的:

With a couple packages, this can be done more elegantly/readably, as suggested by @RichardScriven:

library(data.table)
setDT(DF)[, 
  last_weekdaydate_o_month := last(DATE[!chron::is.weekend(DATE)])
, by = month(DATE)]

其中

           DATE last_weekdaydate_o_month
  1: 2014-01-01               2014-01-31
  2: 2014-01-02               2014-01-31
  3: 2014-01-03               2014-01-31
  4: 2014-01-04               2014-01-31
  5: 2014-01-05               2014-01-31
 ---                                    
361: 2014-12-27               2014-12-31
362: 2014-12-28               2014-12-31
363: 2014-12-29               2014-12-31
364: 2014-12-30               2014-12-31
365: 2014-12-31               2014-12-31

这篇关于R找到上个月的上个月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆