[R] Getting a particular weekday for a given month

(Ted Harding) Ted.Harding at wlandres.net
Mon Apr 7 20:29:45 CEST 2014


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 31        24 25 26 27 28        24 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  6            1  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 31        24 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.Harding at wlandres.net>
Date: 07-Apr-2014  Time: 19:29:41
This message was sent by XFMail




More information about the R-help mailing list