这个
as.POSIXlt
函数提供了一个列表,可以提取该列表来提供类似于C的数字月份值,因为它们以0开头表示一月。这不应该是拆分的问题。有关选项,请参见详细信息部分,但我可以告诉您,没有季度选项。
以下是交付周数的代码:
as.POSIXlt( Sys.Date()+1:60 )$yday %/% 7
[1] 13 13 13 13 13 14 14 14 14 14 14 14 15 15 15 15 15 15 15 16 16 16 16 16 16 16 17 17 17 17
[31] 17 17 17 18 18 18 18 18 18 18 19 19 19 19 19 19 19 20 20 20 20 20 20 20 21 21 21 21 21 21
但请注意,这并不一定与您选择的周初一致。您可能需要减去一年中第一个工作日的数字,以使其正确对齐。(周日是0个工作日。)
as.POSIXlt( Sys.Date()+1:60 - as.POSIXlt( as.Date( paste0( format(Sys.Date(), "%Y"),"-01-01")))$wday )$yday %/% 7
[1] 13 13 13 13 13 13 14 14 14 14 14 14 14 15 15 15 15 15 15 15 16 16 16 16 16 16 16 17 17 17
[31] 17 17 17 17 18 18 18 18 18 18 18 19 19 19 19 19 19 19 20 20 20 20 20 20 20 21 21 21 21 21
另一个提供可打印标签的选项,
zoo::as.yearmon
。
as.POSIXlt( Sys.Date()+1:60 )$mon
[1] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
[46] 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5
zoo::as.yearmon( Sys.Date()+1:60 )
[1] "Apr 2018" "Apr 2018" "Apr 2018" "Apr 2018" "Apr 2018" "Apr 2018" "Apr 2018" "Apr 2018"
[9] "Apr 2018" "Apr 2018" "Apr 2018" "Apr 2018" "Apr 2018" "Apr 2018" "Apr 2018" "Apr 2018"
[17] "Apr 2018" "Apr 2018" "Apr 2018" "Apr 2018" "Apr 2018" "Apr 2018" "Apr 2018" "Apr 2018"
[25] "Apr 2018" "Apr 2018" "Apr 2018" "May 2018" "May 2018" "May 2018" "May 2018" "May 2018"
[33] "May 2018" "May 2018" "May 2018" "May 2018" "May 2018" "May 2018" "May 2018" "May 2018"
[41] "May 2018" "May 2018" "May 2018" "May 2018" "May 2018" "May 2018" "May 2018" "May 2018"
[49] "May 2018" "May 2018" "May 2018" "May 2018" "May 2018" "May 2018" "May 2018" "May 2018"
[57] "May 2018" "May 2018" "Jun 2018" "Jun 2018"
动物园套餐还有
as.yearqtr
功能:
zoo::as.yearqtr( Sys.Date()+seq(0, 180, by=30) )
#[1] "2018 Q2" "2018 Q2" "2018 Q2" "2018 Q3" "2018 Q3" "2018 Q3" "2018 Q3"
“Under the hood”(或“bonnet”(视情况而定))此函数实际上也传递数值,但使用具有特殊打印方法的类:
unclass( zoo::as.yearqtr( Sys.Date()+seq(0, 180, by=30) ) )
[1] 2018.25 2018.25 2018.25 2018.50 2018.50 2018.50 2018.50