如何从日期中获取周数? [英] How to get week numbers from dates?
问题描述
在 R 中寻找将日期转换为周数(年份)的函数,我从包 data.table
中查找了 week
.但是,我观察到了一些奇怪的行为:
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
为什么星期二改为 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?!)
对(基础)R 函数的其他建议也表示赞赏.
Other suggestions for (base) R functions are appreciated as well.
推荐答案
Base package
使用函数 strftime
传递参数 %V
以 ISO 8601 中定义的十进制数 (01–53) 形式获取一年中的第几周.(文档中的更多详细信息:?strftime)
Base package
Using the function strftime
passing the argument %V
to obtain the week of the year as decimal number (01–53) as defined in ISO 8601. (More details in the documentarion: ?strftime)
strftime(c("2014-03-16", "2014-03-17","2014-03-18", "2014-01-01"), format = "%V")
输出:
[1] "11" "12" "12" "01"
这篇关于如何从日期中获取周数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!