如何从日期获取周数? [英] How to get week numbers from dates?
本文介绍了如何从日期获取周数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在R中寻找一个函数将日期转换为周数(一年)我从包中周
data.table
。
然而,我观察到一些奇怪的行为:
>周(2014-03-16)#Sun,expecting 11
[1] 11
> week(2014-03-17)#Mon,expecting 12
[1] 11
> week(2014-03-18)#Tue,expecting 12
[1] 12
为什么星期号切换到星期二12,而不是星期一?我缺少什么? (时区应该不相关,因为只有日期?)
其他建议的(基本)R函数也赞赏。
<如果你尝试使用lubridate:
library(lubridate)
lubridate :: week(ymd(2014-03-16,2014-03-17,2014-03-18,'2014-01-01'))
[1] 11 11 12 1
模式是相同的。尝试 isoweek
lubridate :: isoweek(ymd -16,2014-03-17,2014-03-18,'2014-01-01'))
[1] 11 12 12 1
Looking for a function in R to convert dates into week numbers (of year) I went for week
from package data.table
.
However, I observed some strange behaviour:
> week("2014-03-16") # Sun, expecting 11
[1] 11
> week("2014-03-17") # Mon, expecting 12
[1] 11
> week("2014-03-18") # Tue, expecting 12
[1] 12
Why is the week number switching to 12 on tuesday, instead of monday? What am I missing? (Timezone should be irrelevant as there are just dates?!)
Other suggestions for (base) R functions are appreciated as well.
解决方案
if you try with lubridate:
library(lubridate)
lubridate::week(ymd("2014-03-16", "2014-03-17","2014-03-18", '2014-01-01'))
[1] 11 11 12 1
The pattern is the same. Try isoweek
lubridate::isoweek(ymd("2014-03-16", "2014-03-17","2014-03-18", '2014-01-01'))
[1] 11 12 12 1
这篇关于如何从日期获取周数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文