Reputation: 1408
I have this date vector:
dput(date)
c("1981035", "1981036", "1981037", "1981038", "1981039", "1981040",
"1981041", "1981042", "1981043", "1981044", "1981045", "1981046",
"1981047", "1981048", "1981049", "1981050", "1981051", "1981052",
"1982001", "1982002", "1982003", "1982004", "1982005", "1982006",
"1982007", "1982008", "1982009", "1982010", "1982011", "1982012",
"1982013", "1982014", "1982015", "1982016", "1982017", "1982018",
"1982019", "1982020", "1982021", "1982022", "1982023", "1982024",
"1982025", "1982026", "1982027", "1982028", "1982029", "1982030",
"1982031", "1982032", "1982033", "1982034", "1982035", "1982036",
"1982037")
It is given as yearweek [week 1 covers day-of-the-year 1 to 7]. I want to convert this to format year-month-day and tried the following, but it didn't work:
as.Date(date, "%Y%U")
Upvotes: 0
Views: 158
Reputation: 28685
You have to assign a day of the week for them. Otherwise it cannot be converted to a specific date, since it refers to a range of dates. Choosing day 0 with %w
, i.e. Sunday, you can use the code below.
as.Date(paste0(da, '0'), format = '%Y0%U%w')
Note: This assumes the fifth digit contains no info. That seems odd to me, but is correct according to OP.
Edit: @Kath pointed out it probably makes more sense to think of the data as being in %Y%w%U format, so you can achieve the same result with the simpler code below
as.Date(da, format = '%Y%w%U')
Upvotes: 3