[英]Find month from week numbers using lubridate
I have this list of dates:我有这个日期列表:
library(lubridate)
my.dates = ymd(c("2013-12-14", "2014-01-18", "2014-01-27", "2013-12-13", "2013-12-29", "2013-12-06"))
The following lubridate::week
functions outputs a numeric vector when I convert these dates to week numbers:当我将这些日期转换为周数时,以下lubridate::week
函数会输出一个数字向量:
week(my.dates)
[1] 50 3 4 50 52 49
Can I get lubridate
to output a date ("POSIXct" "POSIXt") object that converts my.dates
to a week number and year number.我可以让lubridate
输出一个日期 ("POSIXct" "POSIXt") 对象,该对象将my.dates
转换为周数和年数。 So output should be a date object (not a character or numeric vector) formatted something like this:所以输出应该是一个日期对象(不是字符或数字向量),格式如下:
[1] "50-2013" "3-2014" "4-2014" "50-2013" "52-2013" "49-2013"
I'm specifically interested in a solution that uses lubridate
.我对使用lubridate
的解决方案特别感兴趣。
To convert my.dates
to a week-year character vector try the following where week
and year
are lubridate functions:要将my.dates
转换为周年字符向量,请尝试以下操作,其中week
和year
是 lubridate 函数:
> paste(week(my.dates), year(my.dates), sep = "-")
[1] "50-2013" "3-2014" "4-2014" "50-2013" "52-2013" "49-2013"
The sample output in the question did not use leading zeros for the week but if leading zeros were desired for the week then:问题中的示例输出没有使用本周的前导零,但如果本周需要前导零,那么:
> sprintf("%02d-%d", week(my.dates), year(my.dates))
[1] "50-2013" "03-2014" "04-2014" "50-2013" "52-2013" "49-2013"
The above are character representations of week-year and do not uniquely identify a date nor can such a format represent a POSIXt object.以上是周年的字符表示,不能唯一标识日期,也不能表示 POSIXt 对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.