24

Possible Duplicate:
Find the day of a week in R

I have a data for days like 11-01-2011 etc. But I want to add the data corresponding the date as Monday, Tuesday etc. Is there any R package that contains the information of the dates with days?

Community
  • 1
  • 1
Ikuyasu
  • 441
  • 1
  • 4
  • 8

3 Answers3

69
weekdays(as.Date('16-08-2012','%d-%m-%Y'))
[1] "Thursday"
Roland
  • 127,288
  • 10
  • 191
  • 288
20

The lubridate package is great for this sort of stuff.

> wday(as.Date('16-08-2012','%d-%m-%Y'))
[1] 5
> wday(as.Date('16-08-2012','%d-%m-%Y'), label=TRUE)
[1] Thurs
Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat
> wday(as.Date('16-08-2012','%d-%m-%Y'), label=TRUE, abbr = FALSE)
[1] Thursday
Levels: Sunday < Monday < Tuesday < Wednesday < Thursday < Friday < Saturday
A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
10

Here is some information to create your own library or routine

Constants:

day_of_month

    the day of the month
    e.g. if input mm-dd-yyy then dd

month:

march = 1
april = 2
may = 3
...

year

yy[yy]  (last to digits from yyyy)
   *subtract 1 if month jan or feb
    e.g. if input date is 02-01-2012 (mm-dd-yyyy)
         year =  (12-1) = 11

century

[yy]yy  (first two digits from yyyy)
     e.g. if input year is 2012 then 20 = century
     * year 2000, 1900, ... are 20-1, 19-1 respectively

ALGORITHM

step1: floor(century / 4)

step2: year

step3: floor(year/4)

step4: floor(month*2.6 -0.2)  #this is the leap year correction

step5: day_of_month

step6: add step1...step5

step7: divide by 7  # modulo 7 in codespeak

step8: the remainder is the day of the week

To Interpret Results:

Sun = 0, Mon = 1, Tues = 3, etc..

Not a library, but as the public service jingle goes...

"Read: The More you Know"

Ref: http://www.faqs.org/faqs/sci-math-faq/dayWeek/

JCR000
  • 966
  • 9
  • 11