R - character string with week-Year: week is lost when converting to Date format
datetime, r, string-formatting
Solution
You just need to add a weekday to your `weeks.strings` in order to make the dates unambiguous (adapted from Jim Holtman's answer on R-help).
as.Date(paste(weeks.strings,1),"%Y-%U %u")
As pointed out in the comments, the Date class is not appropriate if the dates span a long horizon because--at some point--the chosen weekday will not exist in the first/last week of the year. In that case you could use a numeric vector where the whole portion is the year and the decimal portion is the fraction of weeks/year. For example:
wkstr <- sprintf("%d-%02d", rep(2000:2012,each=53), 0:52)
yrwk <- lapply(strsplit(wkstr, "-"), as.numeric)
yrwk <- sapply(yrwk, function(x) x[1]+x[2]/53)
Problem
I have a character string of the date in Year-week format as such: ``` weeks.strings <- c("2002-26", "2002-27", "2002-28", "2002-29", "2002-30", "2002-31") ``` However, converting this character to Date class results in a loss of week identifier: ``` > as.Date(weeks.strings, format="%Y-%U") [1] "2002-08-28" "2002-08-28" "2002-08-28" "2002-08-28" "2002-08-28" [6] "2002-08-28" ``` As shown above, the format is converted into year- concatenated with today's date, so any information about the original week is lost (ex - when using the format function or strptime to try and coerce back into the original format. One solution I found in a help group is to specify the day of the week: ``` as.Date(weeks.strings, format="%Y-%u %U") [1] "2002-02-12" "2002-02-19" "2002-02-26" "2002-03-05" "2002-01-02" [6] "2002-01-09" ``` But it looks like this results in incorrect week numbering (doesn't match the original string). Any guidance would be appreciated.