我有这个日期列表:
library(lubridate)
my.dates = ymd(c("2013-12-14", "2014-01-18", "2014-01-27", "2013-12-13", "2013-12-29", "2013-12-06"))
Run Code Online (Sandbox Code Playgroud)
lubridate::week当我将这些日期转换为周数时,以下函数会输出一个数字向量:
week(my.dates)
[1] 50 3 4 50 52 49
Run Code Online (Sandbox Code Playgroud)
我可以lubridate输出转换my.dates为周数和年份数的日期 ("POSIXct" "POSIXt") 对象吗?所以输出应该是一个日期对象(不是字符或数字向量),格式如下:
[1] "50-2013" "3-2014" "4-2014" "50-2013" "52-2013" "49-2013"
Run Code Online (Sandbox Code Playgroud)
我对使用lubridate.
要转换my.dates为周年字符向量,请尝试以下 whereweek和yearare lubridate 函数:
> paste(week(my.dates), year(my.dates), sep = "-")
[1] "50-2013" "3-2014" "4-2014" "50-2013" "52-2013" "49-2013"
Run Code Online (Sandbox Code Playgroud)
问题中的示例输出没有使用本周的前导零,但如果本周需要前导零,则:
> sprintf("%02d-%d", week(my.dates), year(my.dates))
[1] "50-2013" "03-2014" "04-2014" "50-2013" "52-2013" "49-2013"
Run Code Online (Sandbox Code Playgroud)
以上是周年的字符表示,不能唯一标识日期,也不能表示 POSIXt 对象。
| 归档时间: |
|
| 查看次数: |
2185 次 |
| 最近记录: |