Reputation: 13852
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:
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. 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
.
Upvotes: 1
Views: 2346
Reputation: 270298
To convert my.dates
to a week-year character vector try the following where week
and year
are lubridate functions:
> 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.
Upvotes: 6