[R] Getting a particular weekday for a given month
Hi, 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: library(zoo) 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. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Getting a particular weekday for a given month
Something like: # the third Wednesday m - as.Date(2014-04-01) format(m+which(format(m+0:30,%a) == Wed)[3]-1, %a %b %d) # or eg. all Tuesdays format(m+which(format(m+0:30,%a) == Tue)-1, %a %b %d) # or eg. the last Friday wd - which(format(m+0:30,%a) == Fri)-1 format(m+wd[length(wd)], %a %b %d) Note: adding integers to your Month increments months, not days Cheers, B. On 2014-04-07, at 1:49 PM, Christofer Bogaso wrote: Hi, 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: library(zoo) 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. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Getting a particular weekday for a given month
On 07-Apr-2014 17:49:09 Christofer Bogaso wrote: Hi, 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: library(zoo) 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. The following may not suit you, but it the sort of approach I tend to adopt myself, using things I know about rather than getting lost in R documentation! (Outline of general method, not details). And it also assumes you are using a Unixoid system (e.g. Linux or Mac OS2). Your two commands currently give: library(zoo) Month - as.yearmon(as.Date(Sys.time())) Month # [1] Apr 2014 and it is straightforward to extract Apr and 2014 from Month. This is the point at which I attach my horses to my wooden plough ... In Unixoid systems there is a command 'cal' which, for 2014, yields output: $ cal 2014 2014 January February March Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 4 5 1 2 1 2 6 7 8 9 10 11 12 3 4 5 6 7 8 9 3 4 5 6 7 8 9 13 14 15 16 17 18 19 10 11 12 13 14 15 16 10 11 12 13 14 15 16 20 21 22 23 24 25 26 17 18 19 20 21 22 23 17 18 19 20 21 22 23 27 28 29 30 3124 25 26 27 2824 25 26 27 28 29 30 31 April May June Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 4 5 61 2 3 4 1 7 8 9 10 11 12 13 5 6 7 8 9 10 11 2 3 4 5 6 7 8 14 15 16 17 18 19 20 12 13 14 15 16 17 18 9 10 11 12 13 14 15 21 22 23 24 25 26 27 19 20 21 22 23 24 25 16 17 18 19 20 21 22 28 29 30 26 27 28 29 30 31 23 24 25 26 27 28 29 30 July August September Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 1 2 3 1 2 3 4 5 6 7 7 8 9 10 11 12 13 4 5 6 7 8 9 10 8 9 10 11 12 13 14 14 15 16 17 18 19 20 11 12 13 14 15 16 17 15 16 17 18 19 20 21 21 22 23 24 25 26 27 18 19 20 21 22 23 24 22 23 24 25 26 27 28 28 29 30 31 25 26 27 28 29 30 31 29 30 October November December Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su 1 2 3 4 5 1 2 1 2 3 4 5 6 7 6 7 8 9 10 11 12 3 4 5 6 7 8 9 8 9 10 11 12 13 14 13 14 15 16 17 18 19 10 11 12 13 14 15 16 15 16 17 18 19 20 21 20 21 22 23 24 25 26 17 18 19 20 21 22 23 22 23 24 25 26 27 28 27 28 29 30 3124 25 26 27 28 29 30 29 30 31 After the first two lines, this consists of 4 blocks, each with 8 rows, each covering 3 months where each month consists of 7 columns, one for each day of the week (Mo - Su). Each column occupies 3 character spaces (excpet for the last -- only 2). From April you can readily identify that this is the 4th month, so you need to go to Month 1 of the 2nd block of rows. The We column is the 3rd in that month, and you are looking for the date of the 3rd Wednesday. So count down to the 3rd non-blank entry[*] in this 3rd column, and you will find 16. Done. [*] Some months, e.g. November above, have an initial blank entry because this day belongs to the previous month. Quite how you would program this efficiently in R is another matter! But the principle is simple. To give R a text file to work on, at the shell prompt use a command like: $ cal 2014 cal2014.txt and then cal2014.txt is accessible as a plain text file. Even simpler (if it is only one particular month you want, as in your example) is: $ cal April 2014 which yields: April 2014 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 and now just count down the 3rd column (as before). Maybe this helps ... Ted. - E-Mail: (Ted Harding) ted.hard...@wlandres.net Date: 07-Apr-2014 Time: 19:29:41 This message was sent by XFMail __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Getting a particular weekday for a given month
On Mon, Apr 7, 2014 at 1:49 PM, Christofer Bogaso bogaso.christo...@gmail.com wrote: Hi, 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: library(zoo) 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. In the zoo quickref vignette is a one line nextfri function which can easily be converted to a nextwed function. Note that the code below requires zoo to be loaded as in the first line: library(zoo) d - Sys.Date() d - as.Date(cut(d, month)) # can omit if d is already 1st of month nextwed - function(x) 7 * ceiling(as.numeric(x-3+4) / 7) + as.Date(3-4) nextwed(d) + 14 The last line gives: [1] 2014-04-16 Note that this approach is vectorized and continues to work if d is a Date class vector. -- Statistics Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Getting a particular weekday for a given month
On Mon, 07 Apr 2014, Christofer Bogaso bogaso.christo...@gmail.com writes: Hi, 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: library(zoo) 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 lastWeekday(3, Sys.Date()) 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) ##2012-09-21 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 http://enricoschumann.net/R/packages/PMwR/index.htm If you are on a Unix-type system (or have Rtools installed on Windows), you can directly install the package from source: install.packages('PMwR', repos = 'http://enricoschumann.net/R', type = 'source') Regards, Enrico -- Enrico Schumann Lucerne, Switzerland http://enricoschumann.net __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.