[R] Getting a particular weekday for a given month
es at enricoschumann.net
Tue Apr 8 07:48:49 CEST 2014
On Mon, 07 Apr 2014, Christofer Bogaso <bogaso.christofer at gmail.com> writes:
> Given a month name, I am looking for some script to figure out, what is the
> date for 3rd Wednesday. For example let say I have following month:
> Month <- as.yearmon(as.Date(Sys.time()))
> I need to answer: What is the date for 3rd Wednesday of 'Month'?
> Really appreciate for any pointer.
> Thanks for your time.
There is a function 'lastWeekday' in the PMwR package, which will
compute the last weekday -- Wednesday, say -- in a given month.
(Disclosure: I am the package author.) For example
produces "2014-04-30", which is the last Wednesday of the current month.
To get the third Wednesday of a given month, you can do this:
lastWeekday(3, endOfPreviousMonth(Sys.Date()), shift = 3)
or, for example, for the third Friday of September 2012:
lastWeekday(5, endOfPreviousMonth(as.Date("2012-09-01")), shift = 3)
That is, you first find the last particular weekday of the previous
month, and then shift forward 3 weeks.
However, PMwR is not on CRAN; it's available from here
If you are on a Unix-type system (or have Rtools installed on Windows),
you can directly install the package from source:
repos = 'http://enricoschumann.net/R',
type = 'source')
More information about the R-help