在R中将日期转换为星期几

Iku*_*asu 20 r

可能重复:
在R中查找一周中的某一天

我有一些数据,如11-01-2011等.但我想添加相应日期的数据为星期一,星期二等.是否有任何R包,其中包含日期的日期信息?

Rol*_*and 62

weekdays(as.Date('16-08-2012','%d-%m-%Y'))
[1] "Thursday"
Run Code Online (Sandbox Code Playgroud)

  • 我总是忘记`weekdays()`在基础R. +1 (2认同)

A5C*_*2T1 17

这个lubridate包很适合这种东西.

> 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
Run Code Online (Sandbox Code Playgroud)


JCR*_*000 10

以下是创建自己的库或例程的一些信息

常量:

DAY_OF_MONTH

    the day of the month
    e.g. if input mm-dd-yyy then dd
Run Code Online (Sandbox Code Playgroud)

月份:

march = 1
april = 2
may = 3
...
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

世纪

[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
Run Code Online (Sandbox Code Playgroud)

算法

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
Run Code Online (Sandbox Code Playgroud)

解释结果:

Sun = 0, Mon = 1, Tues = 3, etc..
Run Code Online (Sandbox Code Playgroud)

不是图书馆,而是作为公共服务的叮当...

"阅读:你知道的越多"

参考:http://bit.ly/PqF0M0